http, request, response
The request table, the response table, and outbound calls.
request
| Field | Type | Notes |
|---|---|---|
request.method | string | "GET", "POST", … |
request.path | string | relative to your script |
request.query | table | decoded query parameters |
request.headers | table | lowercase keys |
request.body | string, or a byte array | raw body; a body that is not valid UTF-8 arrives as an array of bytes. Bodies over 10 MB are refused with 413 |
request.uri | string | full uri |
request.upgrade | function or nil | present 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
httpis 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.headerskeys are lowercase.request.headers["Content-Type"]is nil.
Related
- How a request runs: the environment around these tables.
- Websockets: what
request.upgradedoes, and how a script opens a wire outward withClass:open.