Reference · HTTP client

http.put

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

What it does

Sends a PUT with a body, as http.post sends a POST, and gives the answer as a map { status, ok, headers, body, json }. Text goes as text/plain, HTML as text/html, any other value as JSON; null sends no body. Headers come third, options (redirects, public_only, timeout, ca, cert, key) fourth. A 307 or 308 is followed with the same method and body; to another origin, only the accept, accept-language, content-type and user-agent headers go on, as for http.get.

Examples

in a program
let r = http.put(`https://api.example.com/users/${id}`, { name: "Ada", role: "admin" })print(r.ok)http.put(`${bucket}/${key}`, contents, { "content-type": "text/csv" })
print(http.put("http://127.0.0.1:9/users/1", { name: "Ada" }))print(http.put("not a url", "text"))
output
err("http: http://127.0.0.1:9/users/1: Connection Failed: Connect error: Connection refused (os error 111)")err("http: not an http(s) URL: not a url")

Edge cases

print(http.put("http://127.0.0.1:9/users/1"))print(http.put("http://127.0.0.1:9/users/1", "v2", { "if-match": "\"abc\"" }, { timeout: 2 }))
output
err("http: http://127.0.0.1:9/users/1: Connection Failed: Connect error: Connection refused (os error 111)")err("http: http://127.0.0.1:9/users/1: Connection Failed: Connect error: Connection refused (os error 111)")

Errors

Not reaching the server is an error value; a bad argument or option is a runtime error.

let r = http.put("http://127.0.0.1:9/users/1", { name: "Ada" })print(is_err(r), r.error)
output
true http: http://127.0.0.1:9/users/1: Connection Failed: Connect error: Connection refused (os error 111)
this one fails
print(http.put("http://127.0.0.1:9/users/1", {}, "not headers"))
output
line 1: `http.put` expects headers as a map, got str

Chaining

The URL is the first argument. In a route, ? passes an error value on as the request's answer.

in a program
route PUT /users/:id { json: { name: str } } {    http.put(`${upstream}/users/${id}`, { name })?    { ok: true }}
print("not a url" | http.put({ a: 1 }) | is_err)
output
true

See also

http.post, http.patch, http.request, http.get, is_err

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

Thread 0 posts

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