Http Requests - tayjay/SCriPt GitHub Wiki
HTTP Requests
The Http global lets scripts make outbound web requests — for example posting to a Discord webhook, querying an API, or reporting stats.
Disabled by default. HTTP lets a script send data off your server, so it only works when the
HttpEnabledconfig option istrue. You can also restrict which hosts are reachable — see Configuration below.
How it fits the script lifecycle
Requests are asynchronous. Http.Get/Http.Post return immediately and the network call runs in the background; your callback is invoked later, once the response arrives. Crucially, the callback runs back on the game's main thread, so it is safe to touch players, broadcast messages, or change game state from inside it.
The request is tied to the script that started it: if the script unloads (or the server reloads) before the response arrives, the pending callback is simply dropped — it will never fire against an unloaded script.
This means you start requests the same way you do any other work — from an event handler, a command, or a coroutine.
API
| Function | Description |
|---|---|
Http.Get(url, callback) |
Sends a GET request. |
Http.Post(url, body, callback) |
Sends a POST request with body as JSON. |
Http.Post(url, body, contentType, callback) |
Sends a POST with an explicit content type. |
The callback receives three arguments:
function(success, body, status)
success—trueif the request completed with a success status.body— the response text (or an error message whensuccessisfalse).status— the HTTP status code (e.g.200), or0if the request never reached the server.
Example: a command that calls an API
pinger = SCriPt.Module('Pinger')
pinger.command = SCriPt.Command{
type = CommandType.RemoteAdmin,
name = 'ping',
permission = 'script.ping',
execute = function(args, sender)
Http.Get('https://example.com/api/ping', function(success, body, status)
-- Runs later, on the main thread, when the response arrives.
if success then
sender.Respond('Ping OK (' .. status .. '): ' .. body, true)
else
sender.Respond('Ping failed: ' .. body, false)
end
end)
-- We return right away; the callback above answers when the response comes back.
return { response = 'Pinging...', result = true }
end
}
Example: posting to a Discord webhook on round end
reporter = SCriPt.Module('Reporter')
function reporter.load()
reporter.on(Events.Server.RoundEnded, reporter.onRoundEnded)
end
function reporter.onRoundEnded()
local payload = '{"content": "A round just ended with ' .. Server.PlayerCount .. ' players online."}'
Http.Post('https://discord.com/api/webhooks/XXXX/YYYY', payload, function(success, body, status)
if not success then
print('Webhook failed (' .. status .. '): ' .. body)
end
end)
end
Configuration
| Config Option | Description | Default |
|---|---|---|
HttpEnabled |
Master switch — scripts can only make requests when this is true. |
false |
HttpAllowedHosts |
If non-empty, requests are only permitted to these hosts and their subdomains (e.g. discord.com). Leave empty to allow any host. |
empty |
When a request is blocked (HTTP disabled, or host not allowed), the callback is still invoked with success = false and an explanatory body, and a warning is logged to the console.