request
Sends an HTTP request and yields until the response is available.
Syntax
request(options: table) -> tableParameters
| Parameter | Type | Description |
|---|---|---|
options | table | Request configuration |
Options Table
| Field | Type | Description |
|---|---|---|
Url | string | The URL to request |
Method | string? | HTTP method; defaults to GET |
Headers | table? | Request headers |
Body | string? | Request body |
Returns
Response Table
| Field | Type | Description |
|---|---|---|
Success | boolean | Whether the request succeeded |
StatusCode | number | HTTP status code |
StatusMessage | string | HTTP status message |
Headers | table | Response headers |
Body | string | Response body |
Example: GET Request
local response = request({
Url = "https://httpbin.org/get",
Method = "GET"
})
if response.Success then
print("Status:", response.StatusCode)
print("Body:", response.Body)
endExample: POST Request
local HttpService = game:GetService("HttpService")
local response = request({
Url = "https://httpbin.org/post",
Method = "POST",
Headers = {
["Content-Type"] = "application/json"
},
Body = HttpService:JSONEncode({
username = "player",
score = 100
})
})
if response.Success then
local data = HttpService:JSONDecode(response.Body)
print(data)
endError Handling
local success, response = pcall(request, {
Url = "https://example.com/api",
Method = "GET"
})
if success and response.Success then
print("Got data:", response.Body)
elseif success then
warn("HTTP Error:", response.StatusCode, response.StatusMessage)
else
warn("Request failed:", response)
endAliases
http_requesthttp.request