π HTTP
HTTP Requests Interface
info
π¦ Authority: This can be accessed only on Server.
πΏ Static Class: This is a Static Class. You can access itβs methods directly with .
. It is not possible to initialize or create new instances.
Usageβ
Server/Index.lua
-- Makes an asynchronous HTTP Request
HTTP.Request("localhost:7777", "/", "GET", "", "application/json", false, {}, function(status, data)
Package.Log(status) -- 200
Package.Log(data) -- nanos world server
-- TIP: You can parse it if it's a json return as well
local json_ret = JSON.parse(data)
end)
Server/Index.lua
-- Makes a synchronous HTTP Request
local ret = HTTP.RequestSync("localhost:7777", "/", "GET", "", "application/json", false, {})
Package.Log(ret.Status) -- 200
Package.Log(ret.Data) -- nanos world server
tip
All requests are thread safe! π₯³
Static Functionsβ
Returns | Name | Description |
---|---|---|
Request | Makes an asynchronous HTTP Request | |
table | RequestSync | Makes a synchronous HTTP Request |
Request
β
Makes an asynchronous HTTP Request.
The request will be made asynchronously and returned safetly in the same thread in the callback provided when it's done.
Note: If a request is still running when unloading packages, the server will freeze until it's finished, then the package will unload.
HTTP.Request(uri, endpoint, method, content_type, compress, data, headers, callback)
Type | Parameter | Default Value | Description |
---|---|---|---|
string | uri | The main URI | |
string | endpoint | / | The endpoint |
string | method | GET | The HTTP Method to be used |
string | data | | Payload |
string | content_type | application/json | The Content Type to be used |
boolean | compress | false | Whether or not to compress the content with gzip |
table | headers | {} | The Headers to be used |
function | callback | nil | The result will be called in the format function(status, data) |
RequestSync
β
Makes a synchronous HTTP Request.
The request will be made synchronously and will freeze the server until it's done.
Returns a table in the format
{ Status, Data }
HTTP.Request(uri, endpoint, method, content_type, compress, data, headers)
Type | Parameter | Default Value | Description |
---|---|---|---|
string | uri | The main URI | |
string | endpoint | / | The endpoint |
string | method | GET | The HTTP Method to be used |
string | data | | Payload |
string | content_type | application/json | The Content Type to be used |
boolean | compress | false | Whether or not to compress the content with gzip |
table | headers | {} | The Headers to be used |