rest API reference
An ergonomic REST layer over the http client and json. Hold a value-semantic Client (base URL + default headers + an http.Options request policy) and call JSON-aware verbs; the module handles base-URL joining, query strings, Content-Type, and Bearer / Basic auth headers. It is pure composition - no sockets, no parsing of its own - so all the transport lives in http (which uses net), and this module needs the default jennifer binary. A 4xx / 5xx is a normal Response (inspect .status), not a crash. Build a client with rest.client; layer on TLS (rest.withCA / rest.withSkipVerify), a per-request timeout (rest.withTimeout), redirect-following (rest.withRedirects), and retries (rest.withRetries) - each inherited from http.send. For paginated collections, rest.paginate (Link header) and rest.paginateCursor (cursor field) walk every page.
The verbs that decode a body (getJson, paginate, paginateCursor) throw a rest.HttpError on a non-2xx status: kind "rest", the status, the response headers and the body as fields, so a caller branches on $e.status instead of parsing the message. Every error of kind "rest" is an HttpError.
Import with import "rest.j" as rest;. See the rest guide for prose and examples.
Contents
| Name | What it is |
|---|---|
basic | Build an Authorization value for HTTP Basic auth (base64 of "user:password"). |
bearer | Build an Authorization value for a Bearer token. |
client | Build a Client for a base URL, with no default headers and full TLS verification. |
delete | Issue a DELETE with an optional query map. |
get | Issue a GET with an optional query map ({} for none). |
getJson | Issue a GET and decode the response body as JSON. |
paginate | Walk a Link-header paginated collection (GitHub / GitLab style), following each response's... |
paginateCursor | Walk a cursor paginated collection: fetch a page, read the next cursor from the response body at... |
patch | Issue a PATCH with a content type and body. |
patchJson | Issue a PATCH with a JSON body. |
post | Issue a POST with a content type and body. |
postJson | Issue a POST with a JSON body; returns the Response (inspect status). |
put | Issue a PUT with a content type and body. |
putJson | Issue a PUT with a JSON body. |
withBackoff | Return a copy of the client with a base retry backoff in milliseconds (doubled each attempt; 0 = the 250ms ... |
withCA | Return a copy of the client that trusts a private-CA / self-signed PEM certificate (instead of the system r... |
withHeader | Return a copy of the client with one default header set. |
withRedirects | Return a copy of the client that follows up to maxRedirects 3xx redirects (0 = none; the 3xx is returned ... |
withRetries | Return a copy of the client that retries a 429 / 5xx up to maxRetries times with exponential backoff (hon... |
withSkipVerify | Return a copy of the client that skips TLS certificate verification for every https:// request. |
withTimeout | Return a copy of the client with a per-request idle timeout (milliseconds; 0 = the 30s default). |
Client | struct: A REST client: a base URL every path joins onto, default headers sent with every request (auth lives here),... |
HttpError | struct: A non-2xx response where the module needed a JSON body. |
Response | struct: A REST response: the status code, response headers, and the body text. |
Functions
basic
rest.basic(user as string, pass as string)Build an Authorization value for HTTP Basic auth (base64 of "user:password").
user- the username
pass- the password
- Returns
string - the "Basic <base64>" header value
bearer
rest.bearer(token as string)Build an Authorization value for a Bearer token.
token- the bearer token
- Returns
string - the "Bearer <token>" header value
client
rest.client(baseUrl as string)Build a Client for a base URL, with no default headers and full TLS verification. Layer auth / headers on with withHeader and TLS relaxation with withCA / withSkipVerify.
baseUrl- the base URL every path joins onto
- Returns
Client - a new client
delete
rest.delete(c as Client, path as string, query as map of string to string)Issue a DELETE with an optional query map.
c- the client
path- the request path, joined onto the base URL
query- query parameters ({} for none)
- Returns
Response - the response
get
rest.get(c as Client, path as string, query as map of string to string)Issue a GET with an optional query map ({} for none).
c- the client
path- the request path, joined onto the base URL
query- query parameters ({} for none)
- Returns
Response - the response
getJson
rest.getJson(c as Client, path as string, query as map of string to string)Issue a GET and decode the response body as JSON.
c- the client
path- the request path, joined onto the base URL
query- query parameters ({} for none)
- Returns
json.Value - the decoded response body
- Throws
HttpError - kind "rest" on a non-2xx status
paginate
rest.paginate(
c as Client,
path as string,
query as map of string to string,
maxPages as int)Walk a Link-header paginated collection (GitHub / GitLab style), following each response's Link: <url>; rel="next" until it is absent or maxPages is reached, and return the list of decoded page bodies. The first request uses path + query; each next target is resolved against the page's URL and followed as it is (it carries its own query). rel may list several relation types (rel="prev next"). maxPages bounds the walk so a mis-behaving server cannot loop forever. A next link to another origin (scheme, host or port) throws rather than ending the walk early, unless the client's options.allowCrossOriginRedirect is set: following it would send the client's auth headers to a host the server named.
c- the client
path- the first page's path, joined onto the base URL
query- query parameters for the first page ({} for none)
maxPages- the maximum number of pages to fetch
- Returns
list of json.Value - the decoded body of each page, in order
- Throws
HttpError - kind "rest" on a non-2xx page, a malformed Link header, or a next link to another origin (or not http(s))
paginateCursor
rest.paginateCursor(
c as Client,
path as string,
query as map of string to string,
cursorPointer as string,
cursorParam as string,
maxPages as int)Walk a cursor paginated collection: fetch a page, read the next cursor from the response body at cursorPointer (a JSON Pointer, e.g. "/meta/next_cursor"), and re-request with that cursor set as the cursorParam query parameter, until the cursor is absent / null / empty or maxPages is reached. Returns the list of decoded page bodies. The cursor may be a JSON string or integer.
c- the client
path- the collection path, joined onto the base URL
query- the initial query parameters ({} for none)
cursorPointer- a JSON Pointer to the next cursor in each page body
cursorParam- the query-parameter name to send the cursor as
maxPages- the maximum number of pages to fetch
- Returns
list of json.Value - the decoded body of each page, in order
- Throws
HttpError - kind "rest" on a non-2xx page
patch
rest.patch(
c as Client,
path as string,
contentType as string,
body as string)Issue a PATCH with a content type and body.
c- the client
path- the request path, joined onto the base URL
contentType- the
Content-Typeheader value body- the request body
- Returns
Response - the response
patchJson
rest.patchJson(c as Client, path as string, body as json.Value)Issue a PATCH with a JSON body.
c- the client
path- the request path, joined onto the base URL
body- the JSON request body
- Returns
Response - the response
post
rest.post(
c as Client,
path as string,
contentType as string,
body as string)Issue a POST with a content type and body.
c- the client
path- the request path, joined onto the base URL
contentType- the
Content-Typeheader value body- the request body
- Returns
Response - the response
postJson
rest.postJson(c as Client, path as string, body as json.Value)Issue a POST with a JSON body; returns the Response (inspect status).
c- the client
path- the request path, joined onto the base URL
body- the JSON request body
- Returns
Response - the response
put
rest.put(c as Client, path as string, contentType as string, body as string)Issue a PUT with a content type and body.
c- the client
path- the request path, joined onto the base URL
contentType- the
Content-Typeheader value body- the request body
- Returns
Response - the response
putJson
rest.putJson(c as Client, path as string, body as json.Value)Issue a PUT with a JSON body.
c- the client
path- the request path, joined onto the base URL
body- the JSON request body
- Returns
Response - the response
withBackoff
rest.withBackoff(c as Client, backoffMs as int)Return a copy of the client with a base retry backoff in milliseconds (doubled each attempt; 0 = the 250ms default). Pairs with rest.withRetries.
c- the client to copy
backoffMs- the base backoff in milliseconds
- Returns
Client - a new client with the backoff set
withCA
rest.withCA(c as Client, pem as bytes)Return a copy of the client that trusts a private-CA / self-signed PEM certificate (instead of the system roots) for every https:// request. The safer alternative to withSkipVerify: the server is still authenticated.
c- the client to copy
pem- a PEM certificate to trust
- Returns
Client - a new client pinned to the CA
withHeader
rest.withHeader(c as Client, name as string, value as string)Return a copy of the client with one default header set.
c- the client to copy
name- the header name
value- the header value
- Returns
Client - a new client with the header set
withRedirects
rest.withRedirects(c as Client, maxRedirects as int)Return a copy of the client that follows up to maxRedirects 3xx redirects (0 = none; the 3xx is returned as-is). Inherits http.send's redirect rules.
c- the client to copy
maxRedirects- the redirect hop limit
- Returns
Client - a new client that follows redirects
withRetries
rest.withRetries(c as Client, maxRetries as int)Return a copy of the client that retries a 429 / 5xx up to maxRetries times with exponential backoff (honouring a numeric Retry-After). Use rest.withBackoff for a non-default base backoff.
c- the client to copy
maxRetries- the retry limit
- Returns
Client - a new client that retries
withSkipVerify
rest.withSkipVerify(c as Client)Return a copy of the client that skips TLS certificate verification for every https:// request. This disables server authentication and exposes the connection to a man-in-the-middle - use only for a trusted LAN endpoint you cannot give a proper CA; prefer withCA.
c- the client to copy
- Returns
Client - a new client that accepts any certificate
withTimeout
rest.withTimeout(c as Client, timeoutMs as int)Return a copy of the client with a per-request idle timeout (milliseconds; 0 = the 30s default). Bounds each read, so a hung server fails rather than blocking.
c- the client to copy
timeoutMs- the per-read idle timeout in milliseconds
- Returns
Client - a new client with the timeout set
Structs
Client
A REST client: a base URL every path joins onto, default headers sent with every request (auth lives here), and the http.Options request policy (per-request timeout, body cap, redirect-following, retry / backoff, and TLS) applied to every call. Value-semantic; thread it per call.
def struct Client {
baseUrl as string,
headers as map of string to string,
options as http.Options
};baseUrl- the base URL every path joins onto
headers- default headers sent with every request
options- the request policy (timeout, cap, redirects, retries, tls); zero value = defaults, full TLS verification, no redirects / retries
HttpError
A non-2xx response where the module needed a JSON body. Thrown by getJson, paginate and paginateCursor; every error of kind "rest" is one, so its fields are safe to read once the kind matches.
def struct HttpError {
kind as string,
message as string,
status as int,
headers as map of string to string,
body as string
};kind- always
"rest" message<function>: HTTP <status>: <body>, orrest.paginate: <reason>for a Link header it cannot followstatus- the HTTP status code
headers- the response headers (lowercased keys)
body- the response body text
Response
A REST response: the status code, response headers, and the body text.
def struct Response {
status as int,
headers as map of string to string,
body as string
};status- the HTTP status code
headers- the response headers (lowercased keys)
body- the response body text