Vital.sandbox
Rest

rest.post

Shared

Executes a POST request on specified url


Syntax

local response, error = rest.post(url, body, headers = {
    "Content-Type: application/json"
}, timeout = 60)

Must be called within a valid thread context to operate asynchronously and without blocking execution.

  • Inside a thread — runs asynchronously, yielding until the response is received
  • Outside a thread — the request will not be processed and execution may stall
  • Recommended usage — wrap calls in a coroutine or a thread spawned via your runtime's threading API

Parameters

TypeNameDescription
stringurlRequest URL to query
stringbodyRequest body for query
tableheadersRequest headers for query
inttimeoutRead timeout in seconds

Returns

TypeNameDescription
string / boolresponseResponse on successful execution, or false on failure
string / boolerrorfalse on successful execution, or error on failure

Examples

Send a basic POST request with a JSON body
thread:create(function(self)
    local response, errorcode = rest.post(
        "https://api.example.com/users", 
        table.encode({
            name: "John Doe",
            email: "john@example.com"
        }, "JSON")
    )

    engine.print("info", "Response:", response, errorcode)
end):resume()
POST with custom authorization header and timeout
thread:create(function(self)
    local response, errorcode = rest.post(
        "https://api.example.com/users", 
        table.encode({
            name: "John Doe",
            email: "john@example.com"
        }, "JSON"), 
        {
            "Authorization: Bearer my-token"
        },
        30
    )

    engine.print("info", "Response:", response, errorcode)
end):resume()

On this page