Reference · HTTP client
http.put
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
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"))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
- A map or a list goes as JSON, a string as text; a number or a decimal goes as JSON too.
cacheis for GET: on a PUT it is a runtime error.- Every HTTP status is an answer: a 409 for a version conflict has
okfalse and the server'sbody, and is not an error value.
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 }))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)true http: http://127.0.0.1:9/users/1: Connection Failed: Connect error: Connection refused (os error 111)print(http.put("http://127.0.0.1:9/users/1", {}, "not headers"))line 1: `http.put` expects headers as a map, got strChaining
The URL is the first argument. In a route, ? passes an error value on as the
request's answer.
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)trueSee also
http.post, http.patch, http.request, http.get, is_err
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