util.http.post
Shared
Executes a POST request on specified url
Syntax
local promise = util.http.post(
url,
body,
headers = {
"Content-Type: application/json"
},
timeout = 60
)This function returns a promise and starts processing the query. Use thread:await inside a thread to retrieve the result.
- Inside a thread —
self:await(util.http.post(...))yields until the response is received, then returns the resolved values. - Outside a thread — the promise can still be created, but must be awaited from within a thread to retrieve the result.
Parameters
| Type | Name | Description |
|---|---|---|
string | url | Request URL to query |
string | body | Request body for query |
table | headers | Request headers for query |
int | timeout | Read timeout in seconds |
Returns
| Type | Name | Description |
|---|---|---|
promise | promise | Refer Settlements section |
Settlements
| Type | Name | Description |
|---|---|---|
bool | status | true on success, false otherwise |
string | resolved | Response body returned by the server |
string | rejected | Error message describing the failure |
Examples
util.thread.create(function(self)
local ok, result = self:await(util.http.post("https://api.example.com/users",
util.table.encode({
name = "John Doe",
email = "john@example.com"
})
))
if ok then
core.engine.print("info", "Response:", result)
else
core.engine.print("error", "Request failed:", result)
end
end):resume()util.thread.create(function(self)
local ok, result = self:await(util.http.post("https://api.example.com/users",
util.table.encode({
name = "John Doe",
email = "john@example.com"
}),
{
"Authorization: Bearer my-token"
},
30
))
if ok then
core.engine.print("info", "Response:", result)
else
core.engine.print("error", "Request failed:", result)
end
end):resume()