Reference · HTTP client

http.request

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

What it does

Sends a request with any method: the method first, then the URL, a body, a map of headers and a map of options (redirects, public_only, cache for GET, timeout, ca, cert, key). The answer is a map { status, ok, headers, body, json }, as http.get gives. The method is upper-cased; the body is sent as http.post sends one: text as text/plain, HTML as text/html, other values as JSON, null nothing. A 307 or 308 after a method other than GET or HEAD is followed with the same method and body.

Examples

in a program
http.request("PUT", url, body, { authorization: `Bearer ${token}` })http.request("HEAD", url)                       // the headers alone, body ""http.request("PROPFIND", url, null, { depth: "1" })http.request("OPTIONS", url, null, {}, { timeout: 3 })
print(http.request("get", "http://127.0.0.1:9/"))print(http.request("PROPFIND", "not a url"))
output
err("http: http://127.0.0.1:9/: Connection Failed: Connect error: Connection refused (os error 111)")err("http: not an http(s) URL: not a url")

Edge cases

print(http.request("delete", "http://127.0.0.1:9/items/1", { reason: "spam" }))print(http.request("GET", "http://127.0.0.1:9/", null, null, { cache: 60 }))
output
err("http: http://127.0.0.1:9/items/1: Connection Failed: Connect error: Connection refused (os error 111)")err("http: http://127.0.0.1:9/: Connection Failed: Connect error: Connection refused (os error 111)")

Errors

A method that is not a string, a URL that is not one, headers that are not a map and an unknown option are runtime errors; not reaching the server is an error value.

let r = http.request("POST", "http://127.0.0.1:9/")print(is_err(r), r.error)
output
true http: http://127.0.0.1:9/: Connection Failed: Connect error: Connection refused (os error 111)
this one fails
print(http.request(1, "http://127.0.0.1:9/"))
output
line 1: `http.request` expects a method string first, got int
this one fails
print(http.request("POST", "http://127.0.0.1:9/", {}, {}, { cache: 10 }))
output
line 1: `http.request`: `cache` keeps the answers of GET requests, not POST

Chaining

The method comes first, so a pipeline passes the method, not the URL: write the call out. ? passes an error value on to the caller.

in a program
fn call(method: str, path: str, body: any = null) with http {    http.request(method, `${base}${path}`, body, { authorization: key }, { timeout: 10 })?}
print(["GET", "DELETE"] | map((m) => http.request(m, "http://127.0.0.1:9/") | is_err))
output
[true, true]

See also

http.get, http.post, http.put, http.patch, http.delete, http.stream

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

Thread 0 posts

On http.request(): 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