M MY Multiplayer: Wiki
Side
Kind

fetchRemote

Server

Makes an HTTP or HTTPS request and hands the response to a function when it arrives.

bool|string, table fetchRemote(string url, function callback, any ... [, table options = nil])

Parameters

NameTypeDescription
urlstringThe address, starting with http:// or https://. Anything else raises an error.
optionstableOptional table: method (GET, POST, PUT, PATCH, DELETE or HEAD; POST when there is postData and no method), headers (name → value), postData (the body to send) and connectTimeout (in ms, from 1000 to 60000, 10000 if missing). An unknown method raises an error.default nil
callbackfunctionCalled with the response's body, the info table and the extra arguments.
...anyArguments passed to callback after info.

Returns

true if the request was sent. Inside async.thread and without callback, the function waits for the response and returns the body and the info table.

info has success (true when the status is 2xx), statusCode, headers (name → value) and, if the request failed, error. When the resource stops, the responses that have not arrived yet are dropped.

Example

local options = {
    method = "POST",
    headers = { ["Content-Type"] = "application/json" },
    postData = toJSON({ event = "server_started" }),
}

fetchRemote("https://example.com/api/events", options, function(body, info)
    if not info.success then
        outputDebugString("request failed: " .. (info.error or info.statusCode))
    end
end)
Coming from MTAOf MTA's options, only method, headers, postData and connectTimeout are read. Inside async.thread, callback can be left out and the call waits for the response.

See also