Miscellaneous

request

Sends an HTTP or HTTPS request and yields until it completes.

Syntax

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

Arguments

NameTypeDescription
optionsRequestOptionsRequest options table. Url is required; Method, Headers, Cookies, and Body are optional.

Returns

NameTypeDescription
responseResponse | stringOn request error, an error string. Otherwise a response table.

Description

Sends an HTTP or HTTPS request and yields until it completes. options must include Url. Method defaults to GET and must be GET, POST, PUT, PATCH, or DELETE. Headers, Cookies, and Body are optional. Request errors return an error string; completed requests return a response table.

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 GET request and handle either the response table or the request error string.

local response = request({
    Url = "https://example.com/api/status",
    Method = "GET",
})

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 the options table read by request.

Url string Required HTTP or HTTPS URL. The value must start with http:// or https:// and must not match the blocked URL list.Method? "GET" | "POST" | "PUT" | "PATCH" | "DELETE" Optional method. It is normalized before validation.Headers? {[string]: string} Optional request headers. Keys and values must be strings and are rejected if they contain CR or LF. Host, Connection, Content-Length, and Cookie are not forwarded directly.Cookies? {[string]: string} Optional cookies. Keys and values must be strings and are rejected if they contain CR or LF, then merged into the outgoing Cookie header.Body? string Optional request body copied with string conversion. It is only passed to the HTTP client when it is not empty.

Response

Defines the response table returned by request when the request completes without returning an error string.

Success boolean True when StatusCode is between 200 and 299 inclusive.StatusCode integer HTTP status code from the HTTP response.Body string Response body string from the HTTP response.StatusMessage string Status reason string from the HTTP response.RawHeaders string Raw header text reconstructed from the status line, response headers, and Set-Cookie values.Headers {[string]: string} Headers parsed from RawHeaders by splitting lines on the first colon. Later duplicate names overwrite earlier values.Cookies {[string]: string} Cookies parsed from Set-Cookie lines. .ROBLOSECURITY is skipped.