HttpRequest
Sends a web request and hands the answer to a callback.
http:// and https:// addresses only. The callback runs on the server’s simulation thread with status (0 when there was no
answer at all - error then says why), body (the answer as text, 1 MB at most), headers (the answer’s, names in lower case) and
error (nil with an answer). At most 16 requests run at once over the whole server; more wait their turn. Nothing of a request’s
headers or body goes into the server log, so a token can ride in them. A reload of the mode drops the answers still on their way.
| Option | Default | What it is |
|---|---|---|
method |
"GET" |
GET, POST, PUT, PATCH, DELETE or HEAD |
headers |
none | a table of names and values (Authorization, Content-Type …) |
body |
none | the text sent with POST, PUT and PATCH, 1 MB at most |
timeout |
10 |
seconds the answer may take, 1-30 |
Syntax
Section titled “Syntax”HttpRequest(url, callback [, options])| Parameter | Type | |
|---|---|---|
url |
string | the address |
options |
table | the options below; may be left out (the callback then comes second) (optional) |
callback |
function | function(status, body, headers, error) |
Example
Section titled “Example”-- a line in a Discord channel through a webhook when someone joinslocal WEBHOOK = "https://discord.com/api/webhooks/..."
function OnPlayerConnect(pid) HttpRequest(WEBHOOK, { method = "POST", headers = { ["Content-Type"] = "application/json" }, body = JsonEncode({ content = GetPlayerName(pid) .. " joined the server" }), }, function(status, body, headers, err) if status < 200 or status >= 300 then Log("webhook: " .. tostring(err or status)) end end)endSee also
Section titled “See also”GetDiscordMemberRoles · JsonEncode · JsonDecode · the Web requests group of the index
