Utilities

request

Sends an HTTP request with method, headers, body, and timeout options.

Syntax

request(options: RequestOptions): Response | string
Aliases http_requesthttp.request

Arguments

NameTypeDescription
optionsRequestOptionsOptional settings table that controls how the operation behaves.

Returns

NameTypeDescription
responseResponse | stringHTTP response table or compatibility string.

Description

Sends an HTTP request with method, headers, body, and timeout options.

Call it with 1 parameter(s): options. The argument table explains which values are required and which ones only refine the behavior.

It returns response (Response | string). Use the returns table to separate successful values from nil results and recoverable errors.

Example

Send a JSON request and inspect the public response fields.

local response = request({
    Url = "https://example.com/api/status",
    Method = "GET",
    Headers = {
        ["Accept"] = "application/json",
    },
})

if type(response) == "table" then
    if response.Success then
        print(response.StatusCode, response.Body)
    else
        warn(response.StatusMessage)
    end
else
    warn(response)
end

Types

RequestOptions

Defines an HTTP request.

Url string HTTP or HTTPS destination URL.Method? "GET" | "POST" | "PUT" | "PATCH" | "DELETE" HTTP method used for the request.Headers? {[string]: string} Additional request headers.Cookies? {[string]: string} Cookies added to the outgoing request.Body? string Raw request body.