Skip to content
Jennifer Programming Language

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

NameWhat it is
basicBuild an Authorization value for HTTP Basic auth (base64 of "user:password").
bearerBuild an Authorization value for a Bearer token.
clientBuild a Client for a base URL, with no default headers and full TLS verification.
deleteIssue a DELETE with an optional query map.
getIssue a GET with an optional query map ({} for none).
getJsonIssue a GET and decode the response body as JSON.
paginateWalk a Link-header paginated collection (GitHub / GitLab style), following each response's...
paginateCursorWalk a cursor paginated collection: fetch a page, read the next cursor from the response body at...
patchIssue a PATCH with a content type and body.
patchJsonIssue a PATCH with a JSON body.
postIssue a POST with a content type and body.
postJsonIssue a POST with a JSON body; returns the Response (inspect status).
putIssue a PUT with a content type and body.
putJsonIssue a PUT with a JSON body.
withBackoffReturn a copy of the client with a base retry backoff in milliseconds (doubled each attempt; 0 = the 250ms ...
withCAReturn a copy of the client that trusts a private-CA / self-signed PEM certificate (instead of the system r...
withHeaderReturn a copy of the client with one default header set.
withRedirectsReturn a copy of the client that follows up to maxRedirects 3xx redirects (0 = none; the 3xx is returned ...
withRetriesReturn a copy of the client that retries a 429 / 5xx up to maxRetries times with exponential backoff (hon...
withSkipVerifyReturn a copy of the client that skips TLS certificate verification for every https:// request.
withTimeoutReturn a copy of the client with a per-request idle timeout (milliseconds; 0 = the 30s default).
Clientstruct: A REST client: a base URL every path joins onto, default headers sent with every request (auth lives here),...
HttpErrorstruct: A non-2xx response where the module needed a JSON body.
Responsestruct: A REST response: the status code, response headers, and the body text.

Functions

basic

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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-Type header value
body
the request body
Returns Response
the response

patchJson

jennifer
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

jennifer
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-Type header value
body
the request body
Returns Response
the response

postJson

jennifer
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

jennifer
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-Type header value
body
the request body
Returns Response
the response

putJson

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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.

jennifer
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.

jennifer
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>, or rest.paginate: <reason> for a Link header it cannot follow
status
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.

jennifer
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