ServersYour serverFor creatorsForumFAQDocumentationSecurity
Download the launcher
  1. Documentation
  2. Server functions
  3. fetchRemote

Function

fetchRemote

ServerResourcesTrilogy Hub only

Sends an HTTP or HTTPS request and passes the response to a callback without blocking the server. With postData it makes a POST request, without it a GET. The function gets the response body and a code: 0 = success, the HTTP status number on a server error, -1 when there is no connection or it timed out.

Syntax

bool fetchRemote(string url, function callback [, string postData, int timeoutMs = 10000])

Arguments

urlstring
An http:// or https:// address.
callbackfunction
function(responseData, errno).
postDatastring, optional
The POST request body.
timeoutMsint, optional = 10000
Timeout in ms (1000-60000).

Returns

true if the request started.

Example

Fetching a rate from a public API and showing it in chat.

fetchRemote("https://api.example.com/kurs.json", function(data, errno)
    if errno ~= 0 then
        outputServerLog("Błąd zapytania: " .. errno)
        return
    end
    local info = fromJSON(data)
    outputChatBox("Kurs: " .. tostring(info and info.rate))
end)

Notes

Argument order. The callback is the second argument and the POST data the third. The function gets two arguments: the body and the error code.

See also