跳至正文
版本:最新版 - a1.148.0 ⚖️

🌐 HTTP

HTTP 请求接口。

🗿Static ClassClient & Server🧑‍💻API Source

🎒 示例

(HTTP.Request) Makes an asynchronous HTTP Request
local ret = HTTP.Request("127.0.0.1:7777", "/", HTTPMethod.GET, "", "application/json", false, {})

Console.Log(ret.Status) -- 200
Console.Log(ret.Data) -- "{"players_count":0,"server_name":"nanos world server"}"
(HTTP.RequestAsync) Makes an asynchronous HTTP Request
HTTP.RequestAsync("127.0.0.1:7777", "/", HTTPMethod.GET, "", "application/json", false, {}, function(status, data)
Console.Log(status) -- 200
Console.Log(data) -- "{"players_count":0,"server_name":"nanos world server"}"

-- TIP: You can parse it if it's a json return as well
local json_ret = JSON.parse(data)
end)
提示

All requests are thread safe! 🥳 这意味着回调函数在主线程上返回了!

🗿 静态函数

ReturnsNameDescription
tableRequest发起同步 HTTP 请求
RequestAsync发起异步 HTTP 请求
SetConnectionTimeout设置 HTTP 请求的全局连接超时时间(秒)
SetReadWriteTimeout设置 HTTP 请求的全局读写超时时间(秒)

Blocking

Request

发起同步 HTTP 请求。

该请求将同步发出,并会在请求完成前冻结服务器。
local ret = HTTP.Request(uri, endpoint?, method?, data?, content_type?, compress?, headers?)

Parameters

TypeParameterDefaultDescription
stringuri Required parameter 主 URI(基本地址)
stringendpoint?
终点
HTTPMethodmethod?HTTPMethod.GET
要使用的 HTTP 方法
stringdata?
有效载荷
stringcontent_type?application/json
要使用的内容类型
booleancompress?false
是否使用 gzip 压缩内容
tableheaders?{}
要使用的标头

Returns

TypeDescription
tablewith this format
HTTP.Request Examples
Makes an asynchronous HTTP Request
local ret = HTTP.Request("127.0.0.1:7777", "/", HTTPMethod.GET, "", "application/json", false, {})

Console.Log(ret.Status) -- 200
Console.Log(ret.Data) -- "{"players_count":0,"server_name":"nanos world server"}"

Optimal

RequestAsync

发起异步 HTTP 请求。

该请求将异步发出,并在完成后在所提供的回调函数中安全地在相同线程内返回。

注:如果在卸载包时请求仍在运行,服务器将冻结直到请求完成,随后包才会卸载。
HTTP.RequestAsync(uri, endpoint?, method?, data?, content_type?, compress?, headers?, callback?)

Parameters

TypeParameterDefaultDescription
stringuri Required parameter 主 URI(基本地址)
stringendpoint?
终点
HTTPMethodmethod?HTTPMethod.GET
要使用的 HTTP 方法
stringdata?
有效载荷
stringcontent_type?application/json
要使用的内容类型
booleancompress?false
是否使用 gzip 压缩内容
tableheaders?{}
要使用的标头
functioncallback?nil
结果 with this format
HTTP.RequestAsync Examples
Makes an asynchronous HTTP Request
HTTP.RequestAsync("127.0.0.1:7777", "/", HTTPMethod.GET, "", "application/json", false, {}, function(status, data)
Console.Log(status) -- 200
Console.Log(data) -- "{"players_count":0,"server_name":"nanos world server"}"

-- TIP: You can parse it if it's a json return as well
local json_ret = JSON.parse(data)
end)

Optimal

SetConnectionTimeout

设置 HTTP 请求的全局连接超时时间(秒)
HTTP.SetConnectionTimeout(connection_timeout)

Parameters

TypeParameterDefaultDescription
integerconnection_timeout Required parameter 超时时间(秒)

Optimal

SetReadWriteTimeout

设置 HTTP 请求的全局读写超时时间(秒)
HTTP.SetReadWriteTimeout(read_write_timeout)

Parameters

TypeParameterDefaultDescription
integerread_write_timeout Required parameter 超时时间(秒)

🚀 事件

This class doesn't have own events.