跳至正文
版本:latest - a1.147.x ⚖️

🌐 HTTP

HTTP 请求接口。

🗿Static Class
This is a Static Class. Access it's methods directly with .. It's not possible to spawn new instances.
💂Authority
This static class can be accessed on both 🟧 Client and 🟦 Server side.
🧑‍💻API Source
This page is auto-generated! The Functions, Properties and Events described here are defined in our GitHub's API Repository! Feel free to commit suggestions and changes to the source .json API files!

🎒 示例

(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 请求的全局读写超时时间(秒)

Request

Makes a synchronous HTTP Request.

The request will be made synchronously and will freeze the server until it's done.
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"}"

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)

SetConnectionTimeout

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

Parameters

TypeParameterDefaultDescription
integerconnection_timeout Required parameter 超时时间(秒)

SetReadWriteTimeout

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

Parameters

TypeParameterDefaultDescription
integerread_write_timeout Required parameter 超时时间(秒)

🚀 事件

This class doesn't have own events.