http
Server
modkit.http sends HTTP and HTTPS requests from server scripts. Requests run on four worker threads inside the server plugin (WinHTTP). The game thread never waits. The answer reaches your callback on a later tick.
Limits. 128 requests can wait at the same time, a response body is cut at 4 MB, WinHTTP follows redirects on its own (not from HTTPS to HTTP), and the timeout is 15 seconds unless you change it.
modkit.http.request
modkit.http.request(options [, callback])
| Field | Type | Description |
|---|---|---|
url |
string | must start with http:// or https:// |
method |
string | default "GET" |
headers |
table | name -> value |
body |
string | request body |
timeout |
integer | milliseconds, 1000 to 120000, default 15000 |
Returns: the request id. A wrong url, a body that is not a string or a full queue raise an error.
The callback gets one table.
| Field | Type | Description |
|---|---|---|
status |
integer | HTTP status, 0 when the request failed before an answer came |
ok |
boolean | status is 200 to 299 |
body |
string | response body |
headers |
table | response headers, names in lower case |
error |
string or nil | what went wrong, for example "request failed (12029)" with the Windows error code |
modkit.http.request({ url = "https://example.com/" }, function(res)
if res.ok then print("got " .. #res.body .. " bytes") else print("failed", res.status, res.error) end
end)
modkit.http.get, post, postJson
modkit.http.get(url [, callback [, headers]])
modkit.http.post(url, body [, callback [, headers]])
modkit.http.postJson(url, value [, callback [, headers]])
Short forms of request. postJson encodes value with modkit.json.encode and sets Content-Type: application/json.
modkit.http.postJson(modkit.config.get("discord_webhook"), { content = "Server started" })
modkit.http.pending
modkit.http.pending()
Returns: number of requests that are waiting or running.