Reference · HTTP client
http.patch
http.patch(str, any=, map | null=, map | null=) -> map | error(str)What it does
Sends a PATCH with a body, as http.post sends a POST,
and gives the answer as a map { status, ok, headers, body, json }. A map or a list
goes as JSON, text as text/plain, HTML as text/html; 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.patch(`https://api.example.com/users/${id}`, { role: "admin" })print(r.status)http.patch(url, patch, { "content-type": "application/json-patch+json" })print(http.patch("http://127.0.0.1:9/users/1", { role: "admin" }))print(http.patch("not a url", { role: "admin" }))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
- The headers may give the
content-type, for a PATCH format the server wants; the body is still a map sent as JSON text. cacheis for GET: on a PATCH it is a runtime error.- A 422 (the server refused the change) is an answer with
okfalse, not an error value.
print(http.patch("http://127.0.0.1:9/users/1", null, { authorization: "x" }))print(http.patch("http://127.0.0.1:9/users/1", [{ op: "replace", path: "/role", value: "admin" }], {}, { 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.patch("http://127.0.0.1:9/users/1", { role: "admin" })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.patch("http://127.0.0.1:9/users/1", {}, {}, { timeout: 0 }))line 1: `http.patch`: `timeout` is seconds, more than 0 and up to 300, got 0Chaining
The URL is the first argument; ? passes an error value on.
route POST /users/:id/promote { http.patch(`${upstream}/users/${id}`, { role: "admin" })? redirect(`/users/${id}`)}print("not a url" | http.patch({ a: 1 }) | is_err)trueSee also
http.post, http.put, http.request, http.get, is_err
Thread 0 posts
On http.patch(): 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