Namespace: http
Language: Lua
Type: Defold Lua
File: script_http.cpp
Source: engine/gamesys/src/gamesys/scripts/script_http.cpp
Functions for performing HTTP and HTTPS requests.
Type: FUNCTION Perform a HTTP/HTTPS request. If no timeout value is passed, the configuration value “network.http_timeout” is used. If that is not set, the timeout value is 0 (which blocks indefinitely).
Parameters
url (string) - target urlmethod (string) - HTTP/HTTPS method, e.g. “GET”, “PUT”, “POST” etc.callback (function(self, id, response)) - response callback functionselfobject The script instance</dd>
idhash Internal message identifier. Do not use!</dd>
responsetable The response data. Contains the fields:</dd> </dl>
status: the status of the responseresponse: the response data (if not saved on disc)headers: all the returned headers (if status is 200 or 206)path: the stored path (if saved to disc)error: if any unforeseen errors occurred (e.g. file I/O)bytes_received: the amount of bytes received/sent for a request, only if option report_progress is truebytes_total: the total amount of bytes for a request, only if option report_progress is truerange_start: the start offset into the requested filerange_end: the end offset into the requested file (inclusive)document_size: the full size of the requested fileheaders (table) (optional) - optional table with custom headerspost_data (string) (optional) - optional data to sendoptions (table) (optional) - optional table with request parameters. Supported entries:timeout: timeout in secondspath: path on disc where to download the file. Only overwrites the path if status is 200. Path should be absoluteignore_cache: don't return cached data if we get a 304. Not available in HTML5 buildchunked_transfer: use chunked transfer encoding for https requests larger than 16kb. Defaults to true. Not available in HTML5 buildreport_progress: when it is true, the amount of bytes sent and/or received for a request will be passed into the callback functionExamples
Basic HTTP-GET request. The callback receives a table with the response in the fields status, the response (the data) and headers (a table).
local function http_result(self, _, response)
if response.bytes_total ~= nil then
update_my_progress_bar(self, response.bytes_received / response.bytes_total)
else
print(response.status)
print(response.response)
pprint(response.headers)
end
end
function init(self)
http.request("http://www.google.com", "GET", http_result, nil, nil, { report_progress = true })
end