Defold
http
Functions for performing HTTP and HTTPS requests.
Functions
http.request(url: string, method: string, callback: (self: unknown, id: Hash, response: { status: number; url: string; response?: string; headers?: LuaMap<string, string>; path?: string; error?: string; bytes_sent?: number; bytes_received?: number; bytes_total?: number; range_start?: number; range_end?: number; document_size?: number }, sender: Url) => void, headers?: LuaMap<string, string> | Record<string, string>, post_data?: string, options?: { timeout?: number; path?: string; ignore_cache?: boolean; chunked_transfer?: boolean; report_progress?: boolean; proxy?: string }): void
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).
function update_my_progress_bar(self: unknown, fraction: number) {
// draw the progress bar at the given fraction
}
// Basic HTTP-GET request. The callback receives a table with the response
// in the fields status, the response (the data) and headers (a table).
export default defineScript({
init() {
http.request(
"http://www.google.com",
"GET",
(self, _id, response) => {
if (response.bytes_received !== undefined && response.bytes_total !== undefined) {
update_my_progress_bar(self, response.bytes_received / response.bytes_total);
} else {
print(response.status);
print(response.response);
pprint(response.headers);
}
},
undefined,
undefined,
{ report_progress: true },
);
},
});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 })
endParameters
url:string— target urlmethod:string— HTTP/HTTPS method, e.g. "GET", "PUT", "POST" etc.callback:(self: unknown, id: Hash, response: { status: number; url: string; response?: string; headers?: LuaMap<string, string>; path?: string; error?: string; bytes_sent?: number; bytes_received?: number; bytes_total?: number; range_start?: number; range_end?: number; document_size?: number }, sender: Url) => void— response callback functionselfscript_instance The current script instanceidhash Internal message identifier. Do not use!headers?:LuaMap<string, string> | Record<string, string>— optional table with custom headerspost_data?:string— optional data to sendoptions?:{ timeout?: number; path?: string; ignore_cache?: boolean; chunked_transfer?: boolean; report_progress?: boolean; proxy?: string }— optional request options