http API reference
An HTTP/1.1 client over the net system library. Build a request (method, URL, headers, body), send it, and read the response back into a Response (status, headers, body). http:// connects in the clear; https:// connects with TLS (net.connectTLS). The one-shot verbs (get / post / request / ...) use one connection per request (Connection: close); connect / exchange reuse a persistent connection to one origin (keep-alive) so a request loop pays a single handshake. send adds a request policy on top of the one-shot path: automatic 3xx redirect-following, retry / backoff on 429 / 5xx, and a cookie jar across the redirect chain. Because it uses net, this module needs the default jennifer binary. A text Response body is decoded as UTF-8 (a binary body raises an error; use sendBytes / getBytes for that). Chunked and Content-Length framing are both handled.
Import with import "http.j" as http;. See the http guide for prose and examples.
Contents
| Name | What it is |
|---|---|
basic | Build an Authorization value for HTTP Basic auth (base64 of "user:password"), to pass as a request header. |
close | Close a session's connection. |
connect | Open a persistent connection to the origin of url (its scheme / host / port; the path is ignored -... |
defaultOptions | The zero Options (default timeout / cap, verified TLS, no redirects, no retries), for inline use:... |
delete | Issue a DELETE request. |
digestAuthorization | Build an Authorization value answering a Digest challenge (RFC 7616; the RFC 2617 / 2069 forms too). |
exchange | Send one request over a session's reused connection and return the response plus the updated session (reass... |
get | Issue a GET request. |
getBytes | GET a URL and return its body as raw bytes (the download shortcut): sendBytes with zero Options. |
head | Issue a HEAD request (status and headers, no body). |
header | Read a response header by name, case-insensitively. |
isCleartextRemote | Reports whether a request to url would cross the network in the clear: true for http:// to any host but... |
options | Issue an OPTIONS request (capability probe; read the Allow header). |
patch | Issue a PATCH request (a partial update) with contentType and body. |
post | Issue a POST request with contentType and body. |
put | Issue a PUT request with contentType and body. |
request | Send one HTTP request and return the response (default idle timeout and body cap, full TLS verification):... |
requestBytes | Send one request and return a raw-bytes BytesResponse (default idle timeout, default 64 MiB cap, full TLS... |
requestRawBody | Send one request with a raw bytes body and return the text Response. |
requestRawBodyTls | requestRawBody with explicit TLS options for an https:// server. |
requestTls | Send one request with explicit TLS options (default idle timeout and body cap). |
requestWith | Send one HTTP request with an explicit idle timeout and body cap, returning the response. |
requestWithBytes | Send one request and return a raw-bytes BytesResponse, with explicit idle timeout, body cap and TLS options. |
requestWithTls | requestWith with explicit TLS options for an https:// URL. |
send | Send a request with a policy: follow up to options.maxRedirects 3xx redirects, retry a 429 / 5xx up to... |
sendBytes | send with a raw bytes body and a BytesResponse: the body goes onto the wire byte-for-byte (a... |
sendDigest | send with HTTP Digest authentication: the request goes out without credentials, and a 401 carrying a Di... |
BytesResponse | struct: A response with a raw bytes body - the byte-safe counterpart to Response, for downloading binary pa... |
Exchange | struct: The result of an exchange: the response, and the updated session to thread into the next exchange (it c... |
Options | struct: Per-request policy for send and a persistent Session. |
Response | struct: An HTTP response. |
Session | struct: A persistent HTTP connection to one origin (scheme + host + port), holding the reused socket, a cookie jar,... |
TlsOptions | struct: Deprecated TLS options for an https:// request, the fields of net.TLSOptions; set Options.tlsOptions ... |
Functions
basic
http.basic(user as string, pass as string)Build an Authorization value for HTTP Basic auth (base64 of "user:password"), to pass as a request header. Mirrors rest.basic.
user- the username
pass- the password
- Returns
string - the "Basic <base64>" header value
close
http.close(session as Session)Close a session's connection. Idempotent-safe to call once; do not use the session afterwards.
session- the session to close
connect
http.connect(url as string, options as Options)Open a persistent connection to the origin of url (its scheme / host / port; the path is ignored - exchange supplies per-request paths). The socket is reused across exchange calls to the same origin, so a request loop pays one handshake instead of N.
url- a URL whose origin to connect to
options- the request options (timeout, cap, tls)
- Returns
Session - an open session
- Throws
Error - kind "http" for a malformed URL, or a
net.NetError(kind "net") or "timeout" if the connection cannot be opened
defaultOptions
http.defaultOptions()The zero Options (default timeout / cap, verified TLS, no redirects, no retries), for inline use: http.send(m, u, {}, "", http.defaultOptions()). Set fields on the result to opt into behaviour.
- Returns
Options - the default options
delete
http.delete(url as string, headers as map of string to string)Issue a DELETE request.
url- the absolute request URL
headers- request headers ({} for none)
- Returns
Response - the parsed response
digestAuthorization
http.digestAuthorization(
challenge as string,
method as string,
url as string,
user as string,
password as string,
body as string)Build an Authorization value answering a Digest challenge (RFC 7616; the RFC 2617 / 2069 forms too). Picks the strongest supported challenge (SHA-256, then SHA-256-sess, MD5, MD5-sess), qop=auth when offered, else auth-int (which hashes body), with a fresh random client nonce and nc=00000001. For a request made with sendBytes, a Session or any other call: sendDigest does the whole round trip for send.
challenge- the 401 response's
WWW-Authenticatevalue method- the method of the request to authenticate
url- its URL; the
urianswered is its request target user- the user name
password- the password
body- the request body ("" for none)
- Returns
string - the header value, "Digest username=..."
- Throws
Error - kind "http" when no Digest challenge can be answered, or a value would put a control character into the header
exchange
http.exchange(
session as Session,
method as string,
path as string,
headers as map of string to string,
body as string)Send one request over a session's reused connection and return the response plus the updated session (reassign it: def x as http.Exchange init http.exchange($s, ...); $s = $x.session;). path is a request target on the session's origin (e.g. "/items?page=2"). Cookies from the response are folded into the session jar and replayed on later requests. If the connection was closed (by a prior Connection: close or a to-EOF response, or by an exchange that failed partway), it is transparently reopened. When a reused socket turns out stale (the server closed or reset it before any byte of the answer), the request is sent once more on a new connection if that is safe: an idempotent method (GET, HEAD, OPTIONS, TRACE, PUT, DELETE) or an Idempotency-Key header; otherwise the net.NetError is thrown. Redirects are not followed here (that can cross origins and break the socket) - use send for redirect-following; exchange returns the 3xx.
session- the session (from
connector a priorexchange) method- the HTTP method
path- the request path on the session's origin
headers- request headers ({} for none)
body- the request body ("" for none)
- Returns
Exchange - the response and the session to thread onward
- Throws
Error - kind "http" if the response or the request is malformed, "limit" past a cap, "timeout", "closed" for a reply cut short, or a
net.NetError(kind "net") when the network fails (see the module docs, "Errors")
get
http.get(url as string, headers as map of string to string)Issue a GET request.
url- the absolute request URL
headers- request headers ({} for none)
- Returns
Response - the parsed response
getBytes
http.getBytes(url as string, headers as map of string to string)GET a URL and return its body as raw bytes (the download shortcut): sendBytes with zero Options.
url- the absolute request URL
headers- request headers ({} for none)
- Returns
BytesResponse - the response with a raw bytes body
- Throws
Error - as
send
head
http.head(url as string, headers as map of string to string)Issue a HEAD request (status and headers, no body).
url- the absolute request URL
headers- request headers ({} for none)
- Returns
Response - the parsed response (empty body)
header
http.header(resp as Response, name as string)Read a response header by name, case-insensitively.
resp- the response to read from
name- the header name (case-insensitive)
- Returns
string - the header value, or "" if absent
isCleartextRemote
http.isCleartextRemote(url as string)Reports whether a request to url would cross the network in the clear: true for http:// to any host but a loopback one (127.0.0.0/8, [::1], localhost), false for https:// and loopback http://. The host is read the way a request reads it (userinfo cut at the last @), so http://127.0.0.1@evil.example/ is remote. Modules that send credentials use it to refuse plain HTTP unless their caller sets allowInsecureHttp.
url- an http:// or https:// URL
- Returns
bool - true when credentials sent to
urlwould be readable on the wire - Throws
Error - kind "http" for a URL a request would refuse (scheme, host, port)
http.isCleartextRemote("http://192.0.2.7/api"); # true
http.isCleartextRemote("http://127.0.0.1:8080/"); # falseoptions
http.options(url as string, headers as map of string to string)Issue an OPTIONS request (capability probe; read the Allow header).
url- the absolute request URL
headers- request headers ({} for none)
- Returns
Response - the parsed response
patch
http.patch(
url as string,
contentType as string,
body as string,
headers as map of string to string)Issue a PATCH request (a partial update) with contentType and body.
url- the absolute request URL
contentType- the Content-Type header value
body- the request body
headers- extra request headers ({} for none)
- Returns
Response - the parsed response
post
http.post(
url as string,
contentType as string,
body as string,
headers as map of string to string)Issue a POST request with contentType and body.
url- the absolute request URL
contentType- the Content-Type header value
body- the request body
headers- extra request headers ({} for none)
- Returns
Response - the parsed response
put
http.put(
url as string,
contentType as string,
body as string,
headers as map of string to string)Issue a PUT request with contentType and body.
url- the absolute request URL
contentType- the Content-Type header value
body- the request body
headers- extra request headers ({} for none)
- Returns
Response - the parsed response
request
http.request(
method as string,
url as string,
headers as map of string to string,
body as string)Send one HTTP request and return the response (default idle timeout and body cap, full TLS verification): send with zero Options.
method- the HTTP method (e.g. "GET", "POST")
url- the absolute request URL
headers- request headers ({} for none)
body- the request body ("" for no body)
- Returns
Response - the parsed response
- Throws
Error - kind "http" if the response or the request is malformed, "limit" past a cap, "timeout", "closed" for a reply cut short, or a
net.NetError(kind "net") when the network fails (see the module docs, "Errors")
requestBytes
http.requestBytes(
method as string,
url as string,
headers as map of string to string,
body as string)Deprecated: use http.sendBytes with zero http.Options; removed in 0.29.0
Send one request and return a raw-bytes BytesResponse (default idle timeout, default 64 MiB cap, full TLS verification).
method- the HTTP method (e.g. "GET")
url- the absolute request URL
headers- request headers ({} for none)
body- the request body ("" for no body)
- Returns
BytesResponse - the response with a raw bytes body
- Throws
Error - as
send
requestRawBody
http.requestRawBody(
method as string,
url as string,
headers as map of string to string,
body as bytes,
timeoutMs as int,
maxBytes as int)Deprecated: use http.sendBytes with http.Options{timeoutMs, maxBytes}; removed in 0.29.0
Send one request with a raw bytes body and return the text Response.
method- the HTTP method (e.g. "POST")
url- the absolute request URL
headers- request headers (set your own Content-Type)
body- the raw request body
timeoutMs- the idle timeout in milliseconds for each read and each 16 KiB written (0 = none)
maxBytes- the response-body cap (0 = 64 MiB default,
math.MAX_INT= unlimited) - Returns
Response - the parsed response
- Throws
Error - as
send
requestRawBodyTls
http.requestRawBodyTls(
method as string,
url as string,
headers as map of string to string,
body as bytes,
timeoutMs as int,
maxBytes as int,
tls as TlsOptions)Deprecated: use http.sendBytes with http.Options{timeoutMs, maxBytes, tlsOptions};
requestRawBody with explicit TLS options for an https:// server.
method- the HTTP method
url- the absolute request URL
headers- request headers
body- the raw request body
timeoutMs- the idle timeout in milliseconds for each read and each 16 KiB written (0 = none)
maxBytes- the response-body cap (0 = 64 MiB default,
math.MAX_INT= unlimited) tls- certificate-verification options for the TLS handshake
- Returns
Response - the parsed response
- Throws
Error - as
send
requestTls
http.requestTls(
method as string,
url as string,
headers as map of string to string,
body as string,
tls as TlsOptions)Deprecated: use http.send with http.Options{tlsOptions}; removed in 0.29.0
Send one request with explicit TLS options (default idle timeout and body cap).
method- the HTTP method (e.g. "GET", "POST")
url- the absolute request URL
headers- request headers ({} for none)
body- the request body ("" for no body)
tls- certificate-verification options for the TLS handshake
- Returns
Response - the parsed response
- Throws
Error - as
send
requestWith
http.requestWith(
method as string,
url as string,
headers as map of string to string,
body as string,
timeoutMs as int,
maxBytes as int)Deprecated: use http.send with http.Options{timeoutMs, maxBytes}; removed in 0.29.0
Send one HTTP request with an explicit idle timeout and body cap, returning the response. timeoutMs 0 is no idle timeout here (the connect then waits up to 30 s); a negative timeoutMs or maxBytes throws.
method- the HTTP method (e.g. "GET", "POST")
url- the absolute request URL
headers- request headers ({} for none)
body- the request body ("" for no body)
timeoutMs- the idle timeout in milliseconds for each read and each 16 KiB written (0 = none)
maxBytes- the response-body cap (0 = 64 MiB default,
math.MAX_INT= unlimited) - Returns
Response - the parsed response
- Throws
Error - as
send
requestWithBytes
http.requestWithBytes(
method as string,
url as string,
headers as map of string to string,
body as string,
timeoutMs as int,
maxBytes as int,
tls as TlsOptions)Deprecated: use http.sendBytes with http.Options{timeoutMs, maxBytes, tlsOptions};
Send one request and return a raw-bytes BytesResponse, with explicit idle timeout, body cap and TLS options.
method- the HTTP method (e.g. "GET")
url- the absolute request URL
headers- request headers ({} for none)
body- the request body ("" for no body)
timeoutMs- the idle timeout in milliseconds for each read and each 16 KiB written (0 = none)
maxBytes- the response-body cap (0 = 64 MiB default,
math.MAX_INT= unlimited) tls- certificate-verification options for the TLS handshake
- Returns
BytesResponse - the response with a raw bytes body
- Throws
Error - as
send
requestWithTls
http.requestWithTls(
method as string,
url as string,
headers as map of string to string,
body as string,
timeoutMs as int,
maxBytes as int,
tls as TlsOptions)Deprecated: use http.send with http.Options{timeoutMs, maxBytes, tlsOptions};
requestWith with explicit TLS options for an https:// URL.
method- the HTTP method (e.g. "GET", "POST")
url- the absolute request URL
headers- request headers ({} for none)
body- the request body ("" for no body)
timeoutMs- the idle timeout in milliseconds for each read and each 16 KiB written (0 = none)
maxBytes- the response-body cap (0 = 64 MiB default,
math.MAX_INT= unlimited) tls- certificate-verification options for the TLS handshake
- Returns
Response - the parsed response
- Throws
Error - as
send
send
http.send(
method as string,
url as string,
headers as map of string to string,
body as string,
options as Options)Send a request with a policy: follow up to options.maxRedirects 3xx redirects, retry a 429 / 5xx up to options.maxRetries with exponential backoff (honouring Retry-After, seconds or an HTTP-date), and carry cookies across the redirect chain. A 303 (and a 301/302 on a POST) becomes a bodyless GET; 307/308 preserve the method and body. With a zero Options it is a one-shot request, exactly like request.
method- the HTTP method
url- the absolute request URL
headers- request headers ({} for none)
body- the request body ("" for none), sent UTF-8-encoded
options- the request policy
- Returns
Response - the final response (after any redirects / retries)
- Throws
Error - kind "http" if the response or the request is malformed or
optionsis out of range, "limit" past a cap, "timeout", "closed" for a reply cut short, or anet.NetError(kind "net") when the network fails (see the module docs, "Errors")
sendBytes
http.sendBytes(
method as string,
url as string,
headers as map of string to string,
body as bytes,
options as Options)send with a raw bytes body and a BytesResponse: the body goes onto the wire byte-for-byte (a multipart/form-data file upload, a protobuf) and the response body comes back undecoded (a .tar.gz, an image). Redirects, retries and cookies as send.
method- the HTTP method
url- the absolute request URL
headers- request headers ({} for none; set your own Content-Type)
body- the raw request body (empty for none)
options- the request policy
- Returns
BytesResponse - the final response, its body raw bytes
- Throws
Error - as
send
def o as http.Options;
$o.maxBytes = math.MAX_INT; # a large download: no body cap
def none as bytes;
def r as http.BytesResponse init http.sendBytes("GET", $url, {}, $none, $o);sendDigest
http.sendDigest(
method as string,
url as string,
headers as map of string to string,
body as string,
options as Options,
user as string,
password as string)send with HTTP Digest authentication: the request goes out without credentials, and a 401 carrying a Digest challenge is answered once (and once more when the server reports the nonce stale). Any other response, a 401 offering only another scheme included, is returned as is.
The challenge round follows no redirect, so the credentials answer the URL given and no other origin; a 3xx there is returned to the caller. The authenticated request then uses options in full (redirects, retries), and send drops Authorization on a cross-origin hop. The body is sent with both requests.
method- the HTTP method
url- the absolute request URL
headers- request headers ({} for none); an
Authorizationamong them is replaced body- the request body ("" for none)
options- the request policy
user- the user name
password- the password
- Returns
Response - the final response
- Throws
Error - kind "http" as
send, or asdigestAuthorization
Structs
BytesResponse
A response with a raw bytes body - the byte-safe counterpart to Response, for downloading binary payloads (a .tar.gz, an image, any non-text content) that a UTF-8 string body cannot hold. Returned by sendBytes / getBytes. headers keys are lowercased (read $r.headers["content-type"] directly, or with http.header after wrapping - the map is shared shape with Response).
def struct BytesResponse {
status as int,
statusText as string,
headers as map of string to string,
body as bytes
};status- the numeric status code
statusText- the reason phrase from the status line
headers- response headers, keys lowercased
body- the response body, exactly as received (no decoding)
Exchange
The result of an exchange: the response, and the updated session to thread into the next exchange (it carries the reused socket and any new cookies).
def struct Exchange {
response as Response,
session as Session
};response- the response
session- the session to use for the next request
Options
Per-request policy for send and a persistent Session. The zero value (from http.defaultOptions() or def o as http.Options;) is the safe default: the standard timeout and body cap, TLS fully verified, and no redirect-following or retrying (so a bare send behaves like request). Opt into a behaviour by setting its field. A negative number in any field throws Error{kind: "http"}.
def struct Options {
timeoutMs as int,
maxBytes as int,
maxRedirects as int,
maxRetries as int,
backoffMs as int,
tls as TlsOptions,
tlsOptions as net.TLSOptions,
allowCrossOriginRedirect as bool,
allowInsecureRedirect as bool
};timeoutMs- idle timeout for each read and each 16 KiB written, and the connect's deadline (0 = the 30 s default;
math.MAX_INT= no idle timeout, the connect still giving up after 30 s) maxBytes- response-body cap (0 = the 64 MiB default;
math.MAX_INT= unlimited) maxRedirects- how many 3xx redirects to follow (0 = none; the 3xx is returned)
maxRetries- how many times to retry a 429 / 5xx (0 = none), with exponential backoff honouring
Retry-After; any method is retried, aPOSTtoo backoffMs- base backoff for the first retry (0 = 250ms), doubled each attempt and capped at 30s
tls- deprecated, removed in 0.29.0: use
tlsOptions. Setting both throwsError{kind: "http"} tlsOptions- TLS verification options for
https://(zero = full verification against the system roots) allowCrossOriginRedirect- keep every header and the cookies across a redirect to a different origin (default false = keep only content-negotiation, caching and range headers, and drop the cookies)
allowInsecureRedirect- follow a redirect from
https://to plainhttp://(default false = throwError{kind: "http"}instead of sending the next request in the clear)
Response
An HTTP response. headers keys are lowercased (HTTP header names are case-insensitive); use http.header for a case-insensitive read.
def struct Response {
status as int,
statusText as string,
headers as map of string to string,
body as string
};status- the numeric status code (e.g. 200, 404)
statusText- the reason phrase from the status line
headers- response headers, keys lowercased
body- the response body decoded as UTF-8 text
Session
A persistent HTTP connection to one origin (scheme + host + port), holding the reused socket, a cookie jar, and the request options. Value-semantic, but the conn handle is shared across copies (a net.Conn), so the live socket survives being threaded through exchange. Build with connect; retire with close.
def struct Session {
conn as net.Conn,
scheme as string,
host as string,
port as int,
open as bool,
jar as map of string to string,
options as Options
};conn- the underlying socket (shared across copies)
scheme- "http" or "https"
host- the origin host
port- the origin port
open- whether
connis currently usable (false after the server closed it) jar- the accumulated cookies (name -> value)
options- the request options (timeout, cap, tls)
TlsOptions
Deprecated: use net.TLSOptions in Options.tlsOptions; removed in 0.29.0
Deprecated TLS options for an https:// request, the fields of net.TLSOptions; set Options.tlsOptions (a net.TLSOptions) instead. Removed in 0.29.0. The zero value full-verifies the server certificate against the URL host and the system roots.
def struct TlsOptions {
skipVerify as bool,
caCert as bytes
};skipVerify- accept any certificate (self-signed, wrong host, expired). Opt-in; disables authentication and exposes the connection to a man-in-the-middle. Use only for a trusted LAN endpoint you cannot give a proper CA.
caCert- a PEM certificate (or bundle) to trust instead of the system roots, for a private CA or a pinned self-signed cert. The safer alternative to
skipVerify: the server is still authenticated, just against this cert.