Skip to main content

HttpClient

Description:

  Represents an HTTP client.

post

Type: Function.

Description:

  Starts a non-blocking POST request with a JSON body.

Signature:

post: function(self: HttpClient, url: string, json: string, timeout: number, callback: function(data: string | nil)): integer

Parameters:

ParameterTypeDescription
urlstringThe URL to make the request to.
jsonstringThe JSON text to send in the request.
timeoutnumber[optional] The request timeout in seconds (default is 5 seconds).
callbackfunctionCalled when the request finishes. It receives the response text, or nil if the request fails or is cancelled.

Returns:

Return TypeDescription
integerThe request handle, or 0 if the request cannot be scheduled.

post

Type: Function.

Description:

  Starts a non-blocking POST request with custom headers and a JSON body.

Signature:

post: function(self: HttpClient, url: string, headers: {string}, json: string, timeout: number, callback: function(data: string | nil)): integer

Parameters:

ParameterTypeDescription
urlstringThe URL to make the request to.
headers{string}The headers to send in the request. Each header should be in the format "name: value".
jsonstringThe JSON text to send in the request.
timeoutnumber[optional] The request timeout in seconds (default is 5 seconds).
callbackfunctionCalled when the request finishes. It receives the response text, or nil if the request fails or is cancelled.

Returns:

Return TypeDescription
integerThe request handle, or 0 if the request cannot be scheduled.

post

Type: Function.

Description:

  Starts a non-blocking POST request with custom headers and a JSON body, and optionally consumes the response stream in chunks before completion.

Signature:

post: function(self: HttpClient, url: string, headers: {string}, json: string, timeout: number, partCallback: function(data: string): boolean, callback: function(data: string | nil)): integer

Parameters:

ParameterTypeDescription
urlstringThe URL to make the request to.
headers{string}The headers to send in the request. Each header should be in the format "name: value".
jsonstringThe JSON text to send in the request.
timeoutnumber[optional] The request timeout in seconds (default is 5 seconds).
partCallbackfunction[optional] Called when response chunks arrive. Return true to stop and cancel the request early.
callbackfunctionCalled when the request finishes. It receives the full response text, or nil if the request fails or is cancelled.

Returns:

Return TypeDescription
integerThe request handle, or 0 if the request cannot be scheduled.

postAsync

Type: Function.

Description:

  Sends a POST request with a JSON body and waits for the response text.

Signature:

postAsync: function(self: HttpClient, url: string, json: string, timeout?: number): string | nil

Parameters:

ParameterTypeDescription
urlstringThe URL to make the request to.
jsonstringThe JSON text to send in the request.
timeoutnumber[optional] The request timeout in seconds (default is 5 seconds).

Returns:

Return TypeDescription
stringThe response text, or nil if the request fails.

postAsync

Type: Function.

Description:

  Sends a POST request with custom headers and a JSON body, and optionally consumes the response stream in chunks before completion.

Signature:

postAsync: function(self: HttpClient, url: string, headers: {string}, json: string, timeout?: number, partCallback?: function(data: string): boolean): string | nil

Parameters:

ParameterTypeDescription
urlstringThe URL to make the request to.
headers{string}The headers to send in the request. Each header should be in the format "name: value".
jsonstringThe JSON text to send in the request.
timeoutnumber[optional] The request timeout in seconds (default is 5 seconds).
partCallbackfunction[optional] Called when response chunks arrive. Return true to stop and cancel the request early.

Returns:

Return TypeDescription
stringThe response text, or nil if the request fails.

get

Type: Function.

Description:

  Starts a non-blocking GET request.

Signature:

get: function(self: HttpClient, url: string, timeout: number, callback: function(data: string | nil)): integer

Parameters:

ParameterTypeDescription
urlstringThe URL to make the request to.
timeoutnumber[optional] The request timeout in seconds (default is 5 seconds).
callbackfunctionCalled when the request finishes. It receives the response text, or nil if the request fails or is cancelled.

Returns:

Return TypeDescription
integerThe request handle, or 0 if the request cannot be scheduled.

getAsync

Type: Function.

Description:

  Sends a GET request and waits for the response text.

Signature:

getAsync: function(self: HttpClient, url: string, timeout?: number): string | nil

Parameters:

ParameterTypeDescription
urlstringThe URL to make the request to.
timeoutnumber[optional] The request timeout in seconds (default is 5 seconds).

Returns:

Return TypeDescription
stringThe response text, or nil if the request fails.

download

Type: Function.

Description:

  Starts a non-blocking file download.

Signature:

download: function(self: HttpClient, url: string, fullPath: string, timeout?: number, progress?: function(interrupted: boolean, current: integer, total: integer): boolean): integer

Parameters:

ParameterTypeDescription
urlstringThe URL of the file to download.
fullPathstringThe full path where the downloaded file should be saved.
timeoutnumber[optional] The download timeout in seconds (default is 30 seconds).
progressfunction[optional] Reports download progress with interrupted, current, and total. Return true to cancel the download.

Returns:

Return TypeDescription
integerThe request handle, or 0 if the request cannot be scheduled.

downloadAsync

Type: Function.

Description:

  Downloads a file and waits until it finishes. This method must be called from a coroutine/thread.

Signature:

downloadAsync: function(self: HttpClient, url: string, fullPath: string, timeout?: number, progress?: function(current: integer, total: integer): boolean): boolean

Parameters:

ParameterTypeDescription
urlstringThe URL of the file to download.
fullPathstringThe full path where the downloaded file should be saved.
timeoutnumber[optional] The download timeout in seconds (default is 30 seconds).
progressfunction[optional] Reports download progress with current and total byte counts. Return true to cancel the download.

Returns:

Return TypeDescription
booleantrue if the download finishes successfully; otherwise false.

cancel

Type: Function.

Description:

  Requests cancellation for an in-flight request.

Signature:

cancel: function(self: HttpClient, requestId: integer): boolean

Parameters:

ParameterTypeDescription
requestIdintegerThe request handle returned by post, get, or download.

Returns:

Return TypeDescription
booleantrue if the request was found and cancellation was requested.

isRequestActive

Type: Function.

Description:

  Checks whether a request is still active.

Signature:

isRequestActive: function(self: HttpClient, requestId: integer): boolean

Parameters:

ParameterTypeDescription
requestIdintegerThe request handle returned by post, get, or download.

Returns:

Return TypeDescription
booleantrue if the request is still running.