跳至正文
版本:bleeding-edge 🩸

🌐 HTTP

HTTP 请求接口。

🗿Static ClassClient & Server🧑‍💻API Source

🎒 示例​

(HTTP.Request) Makes a synchronous 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"}"
Console.Log(ret.Headers["Content-Type"]) -- "application/json"
(HTTP.RequestAsync) Makes an asynchronous HTTP Request
HTTP.RequestAsync("127.0.0.1:7777", "/", HTTPMethod.GET, "", "application/json", false, {}, function(status, data, headers)
Console.Log(status) -- 200
Console.Log(data) -- "{"players_count":0,"server_name":"nanos world server"}"
Console.Log(headers["Content-Type"]) -- "application/json"

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

所有请求都是线程安全的! 🥳 这意味着回调函数在主线程上返回了!

🗿 静态函数​

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
table包含响应状态 <code>Status</code>、数据 <code>Data</code> 及响应头 <code>Headers</code> 的表 with this format
HTTP.Request Examples
Makes a synchronous 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"}"
Console.Log(ret.Headers["Content-Type"]) -- "application/json"

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, headers)
Console.Log(status) -- 200
Console.Log(data) -- "{"players_count":0,"server_name":"nanos world server"}"
Console.Log(headers["Content-Type"]) -- "application/json"

-- 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.