HTTP
The HTTP library provides functions for making HTTP requests from Lua scripts.
Global Functions
httpget(url)
Performs an asynchronous HTTP GET request.
url
: The URL to send the request to.
Returns: Yields and resumes with the response body as a string, or an error message.
httppost(url, data, [content_type])
Performs an asynchronous HTTP POST request.
url
: The URL to send the request to.data
: The data to send in the request body.content_type
: (Optional) The content type of the request. Defaults to "/".
Returns: Yields and resumes with the response body as a string, or an error message.
local response = httppost("https://example.com/api", "key=value", "application/x-www-form-urlencoded")
print(response)
request(options)
Performs an asynchronous HTTP request with more control over the request parameters.
options
: A table with the following fields:
Field | Type | Required | Description |
---|---|---|---|
Url |
string | Yes | The URL to send the request to |
Method |
string | No | The HTTP method to use (default: "GET") |
Headers |
table | No | A table of request headers |
Body |
string | Yes (for POST/PUT/PATCH) | The request body |
Cookies |
table | No | A table of cookies to send with the request |
Returns: Yields and resumes with a table containing the response details.
local response = request({
Url = "https://example.com/api",
Method = "POST",
Headers = {["Content-Type"] = "application/json"},
Body = '{"key": "value"}',
Cookies = {session = "abc123"}
})
print(response.Body)
The response table contains the following fields:
Field | Type | Description |
---|---|---|
Success |
boolean | Indicates if the request was successful (status code 200-299) |
StatusCode |
number | The HTTP status code of the response |
StatusMessage |
string | The status message accompanying the status code |
Headers |
table | A table of response headers |
Body |
string | The response body |
http Table
The http
table provides an alternative way to access the request
function:
http.request(options)
Identical to the global request
function.