ACTIAS

http, request, response

The request table, the response table, and outbound calls.

request

FieldTypeNotes
request.methodstring"GET", "POST", …
request.pathstringrelative to your script
request.querytabledecoded query parameters
request.headerstablelowercase keys
request.bodystring, or a byte arrayraw body; a body that is not valid UTF-8 arrives as an array of bytes. Bodies over 10 MB are refused with 413
request.uristringfull uri
request.upgradefunction or nilpresent on websocket handshakes

request.path has the routing prefix removed: a script served at /hello/ sees /api/thing, not /hello/api/thing.

response

return {
    status_code = 200,          -- defaults to 200
    headers = { ["content-type"] = "application/json" },
    body = json.stringify({ ok = true }),
}

body takes a string or a byte array, so getfile output can be returned directly.

Outbound requests

local response = http.make_request({
    uri = "https://example.com/api",
    method = "POST",
    headers = { ["content-type"] = "application/json" },
    body = json.stringify({ hello = "world" }),
})

response.status_code
response.headers
response.body      -- a string, or a byte array when not valid UTF-8

Outbound calls go through a guarded client. A refused or unreachable host raises an error, so wrap calls that may fail in pcall or let a queue handler retry them.

Uri

local uri = Uri.parse("https://example.com/a?b=c")   -- also http.Uri.parse
uri.scheme       -- "https"
uri.authority    -- "example.com"
uri.path         -- "/a"
uri.query        -- "b=c"
uri:tostring()

Uri.new({ scheme = "https", authority = "example.com", path = "/a" })

Gotchas

  • http is absent from a workflow environment, step bodies included. Call an object from a step to reach the network.
  • Waiting on a response spends no work budget, but it does spend the request's ten second wall backstop and its 30 second deadline. A slow host takes the whole request down with it.
  • request.headers keys are lowercase. request.headers["Content-Type"] is nil.
  • How a request runs: the environment around these tables.
  • Websockets: what request.upgrade does, and how a script opens a wire outward with Class:open.