Skip to Content
defold-typescript
Get startedGuidesAPILibraries
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 })
end

Parameters

  • url: string — target url
  • method: 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 function self script_instance The current script instance id hash Internal message identifier. Do not use!
  • headers?: LuaMap<string, string> | Record<string, string> — optional table with custom headers
  • post_data?: string — optional data to send
  • options?: { timeout?: number; path?: string; ignore_cache?: boolean; chunked_transfer?: boolean; report_progress?: boolean; proxy?: string } — optional request options

On this page