Reference · HTTP client

http.post

signature
http.post(str, any=, map | null=, map | null=) -> map | error(str)

What it does

Sends a POST with a body and gives the answer as a map { status, ok, headers, body, json }, as http.get does. The body is sent as it is when it is text (text/plain) or HTML (text/html), and as JSON otherwise (a map, a list, a number, a decimal); null sends no body. Headers come third and may give another content-type; the options (redirects, public_only, timeout, ca, cert, key) come fourth. A 307 or 308 answer is followed with the same method and body, as fetch does; a 301, 302 or 303 is followed as a GET without the body, as browsers do. To another origin (scheme, host and port), only the accept, accept-language, content-type and user-agent headers are sent on (since 0.7).

Examples

in a program
let r = http.post("https://api.example.com/users", { name: "Ada" })print(r.status, r.json.id)http.post(hook, "deploy finished")                                 // text/plainhttp.post(url, "a=1&b=2", { "content-type": "application/x-www-form-urlencoded" })http.post(url, body, { authorization: `Bearer ${key}` }, { timeout: 10 })
print(http.post("http://127.0.0.1:9/users", { name: "Ada" }))print(http.post("not a url", { name: "Ada" }))
output
err("http: http://127.0.0.1:9/users: Connection Failed: Connect error: Connection refused (os error 111)")err("http: not an http(s) URL: not a url")

Edge cases

print(http.post("http://127.0.0.1:9/ping"))print(http.post("http://127.0.0.1:9/ping", null, null, { redirects: 0 }))print(http.post("http://127.0.0.1:9/ping", "hello", {}, { public_only: true }))
output
err("http: http://127.0.0.1:9/ping: Connection Failed: Connect error: Connection refused (os error 111)")err("http: http://127.0.0.1:9/ping: Connection Failed: Connect error: Connection refused (os error 111)")err("http: http://127.0.0.1:9/ping: refused by public_only: 127.0.0.1 is not on the public internet")

Errors

Not reaching the server is an error value; the arguments are checked at once as runtime errors: a URL that is not a string, headers that are not a map, an option that does not exist, cache.

let r = http.post("http://127.0.0.1:9/users", { name: "Ada" })print(is_err(r), r.error)print(http.post("http://127.0.0.1:9/users", {}) ?? "queued for later")
output
true http: http://127.0.0.1:9/users: Connection Failed: Connect error: Connection refused (os error 111)queued for later
this one fails
print(http.post("http://127.0.0.1:9/users", {}, {}, { cache: 10 }))
output
line 1: `http.post`: `cache` keeps the answers of GET requests, not POST

Chaining

The URL comes first in a pipeline, so the body cannot be piped in; write the call out. ? passes an error value on, and .json reads the answer.

in a program
route POST /signup { form: { email: str } } {    let r = http.post(`${crm}/contacts`, { email }, { authorization: key })?    if !r.ok { reply 502 "the CRM is down" }    redirect("/welcome")}
print("not a url" | http.post({ a: 1 }) | is_err)
output
true

See also

http.put, http.patch, http.get, http.request, json.stringify, is_err

Thread
8Useful? Like it, or add to its thread below.

Thread 0 posts

On http.post(): questions, tips, corrections, and code that uses it.

No posts yet. The first one opens the thread.

    Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo