net.http

HTTP/HTTPS client helpers backed by the eta-http native sidecar package (packages/net/native/http) and libcurl.

(import net.http)

net.http is opt-in. Add the eta-http package to your app dependencies.

Session

SymbolDescription
(make-session . opts)Create a reusable HTTP session handle with optional default options.
(session? value)Predicate for session handles.
(close-session! session)Close a session early and release native resources.
(session-set-option! session name value)Set a session option.
(session-get-option session name)Read one session option value.

Request builder

SymbolDescription
(make-request session method)Create a request handle bound to session.
(request? value)Predicate for request handles.
(request-set-url! request url)Set request URL.
(request-set-option! request name value)Override one option for this request only.
(request-set-header! request name value-or-false)Add/update a header, or remove it with #f.
(request-set-body! request text)Set string body (UTF-8).
(request-set-body-bytes! request bytes)Set bytevector body.
(request-set-body-string! request text charset)Set string body with explicit charset label.
(request-set-body-file! request path)Stream body from file path.
(request-set-form! request fields)Submit URL-encoded form body from alist.
(request-set-multipart! request parts)Submit multipart/form-data parts.
(perform request)Execute request and return response handle.
(perform-stream request)Execute request through streaming path.

Response

SymbolDescription
(response? value)Predicate for response handles.
(status response)HTTP status code.
(ok? response)#t for 2xx status.
(raise-for-status! response)Raise runtime error on non-2xx status.
(body-bytes response)Body as bytevector.
(body-string response)Body decoded byte-by-byte to string.
(body-json response)Parse body as JSON via std.json.
(headers response)Header alist (name . value).
(header response name)First matching header value or #f.
(effective-url response)Final URL after redirects.

Convenience helpers

SymbolDescription
(get url . rest)One-shot GET (rest is optional opts and/or session).
(get-json url . rest)One-shot GET plus JSON decode.
(post url body . rest)One-shot POST string body.
(post-json url value . rest)One-shot POST with JSON-encoded body.
(download url path . rest)Stream payload to file path.

URL helpers

SymbolDescription
(url-encode text)Percent-encode text.
(url-decode text)Percent-decode text.
(url-parse text)Parse URL to component alist.
(url-build components)Build URL from component alist.

Common options

Options are set with session-set-option! or request-set-option!.

OptionTypeDescription
'follow-redirectsboolEnable redirect following.
'max-redirectsintRedirect cap.
'connect-timeout-msintConnect timeout in milliseconds.
'timeout-msintTotal request timeout in milliseconds (0 means no cap).
'user-agentstringUser-Agent header value.
'accept-encodingstringAccepted compression list ("" lets libcurl negotiate).
'verify-tlsboolTLS certificate/host verification toggle.
'ca-bundle / 'ca-pathpathCustom trust bundle/path (backend dependent).
'username / 'passwordstringBasic auth credentials.
'bearer-tokenstringBearer token.
'proxystringProxy URL.
'http-versionsymbolHTTP version preference.
'verboseboolEnable verbose transfer logging from libcurl.

Relationship to std.net

std.net is message-passing over NNG (Eta-to-Eta patterns). net.http is client HTTP/HTTPS over libcurl (Eta-to-web-service patterns). They are complementary surfaces and can be used together in one application.

Notes