NAME
std/net/http - HTTP/web client foundation.
SYNOPSIS
from std/net/http import CookieJar, UserAgent;
let jar := new CookieJar();
let ua := new UserAgent(
cookie_jar: jar,
agent: "my-app/1.0"
);
let req := ua
.build_request("GET", "https://example.com/items")
.query({ page: "1" })
.auth_bearer("TOKEN");
let res := ua.send(req);
let data := res.expect_success().json();
IMPLEMENTATION SUPPORT
This module is supported by zuzu.pl, zuzu-rust, and zuzu-js on Node and Electron. It is partially supported by zuzu-js in the browser: asynchronous HTTP, cookie jar, header normalization, URL, timeout, and retry coverage passes, but synchronous request coverage is unsupported.
DESCRIPTION
This module provides an HTTP client API with classes for cookie handling, request building, retries/timeouts, and JSON-first responses.
EXPORTS
Classes
CookieJar(config?)Creates a cookie jar. Returns:
CookieJar.jar.add(String url, String set_cookie)Parameters:
urlis the source URL andset_cookieis aSet-Cookieheader value. Returns:CookieJar. Stores cookies from the header.jar.cookie_header(String url)Parameters:
urlis a request URL. Returns:Stringornull. Builds theCookieheader value forurl.jar.clear()Parameters: none. Returns:
CookieJar. Removes all stored cookies.
UserAgent(config?)Creates an HTTP user agent. Returns:
UserAgent.Supported config keys include
default_headers,cookie_jar,max_redirect,timeout, and TLS policy keys:tls_identity,tls_ca,tls_verify,tls_server_name,tls_min_version, andtls_ciphers.tls_identityaccepts astd/secureTlsIdentityornull.tls_caaccepts astd/secureCertificate, PEM certificate text, an array of those, ornull.tls_verifydefaults to true.tls_min_versionaccepts"tls1.2"or"tls1.3".tls_ciphersis a backend-native cipher string.Backends throw a clear unsupported error for non-default TLS settings they cannot honour. JS/Browser does not support script-selected TLS client configuration; any non-default TLS policy throws when sending.
Builder and dispatch methods:
build_request(method, url)Parameters:
methodis an HTTP method andurlis the request URL. Returns:Request. Builds a mutable request.send(request)Parameters:
requestis aRequest. Returns:Response. Sends the request synchronously.send_async(request)Parameters:
requestis aRequest. Returns:Task. Sends the request asynchronously.
Request methods:
- Generic
request(method, url, data?, headers?)Parameters:
methodis an HTTP method,urlis the request URL,datais optional request data, andheadersare optional headers. Returns:Response. Builds and sends one request. - Async generic
request_async(method, url, data?, headers?)Parameters: same as
request. Returns:Task. Asynchronous request helper resolving toResponse. - Convenience
get,head,delete,options,post,put,patch.get_async,head_async,delete_async,options_async,post_async,put_async, andpatch_asyncreturn awaitableTaskvalues which resolve toResponse.For
post,put, andpatch, pass data then optional headers. For the others, the second argument can be headers.
RequestMutable request builder. Returns:
Requestwhen constructed by the user agent.request.method(value?),request.url(value?)Parameters: optional
valueupdates the method or URL. Returns:Stringwhen reading, orRequestwhen setting.request.header(name, value?)Parameters:
nameis a header name andvalueis optional. Returns:StringorRequest. Gets or sets one request header.request.headers(value?)Parameters: optional
valueis header data. Returns: header data orRequest. Gets or replaces request headers.request.query(values)Parameters:
valuesis query parameter data. Returns:Request. Adds query parameters to the request URL.request.body(value?),request.content(value?)Parameters: optional
valueis request body data. Returns: body value orRequest. Gets or sets the request body.request.json(value)Parameters:
valueis JSON-encodable data. Returns:Request. Sets a JSON request body and content type.request.auth_bearer(String token)Parameters:
tokenis a bearer token. Returns:Request. Sets theAuthorizationheader.request.timeout(Number seconds),request.retries(Number count)Parameters: numeric values configure request behaviour. Returns:
Request. Sets timeout or retry policy.request.download_to(path),request.upload_from(path)Parameters:
pathis astd/ioPathor compatible path value. Returns:Request. Configures response download or request upload.request.tls_identity(identity_or_null)Parameters:
identity_or_nullis a TLS identity ornull. Returns:Request. Overrides the user-agent TLS identity for the request.request.multipart(parts)Parameters:
partsdescribes multipart form parts. Returns:Request. Sets a multipart request body.request.send(user_agent)Parameters:
user_agentis aUserAgent. Returns:Response. Sends the request synchronously.request.send_async(user_agent)Parameters:
user_agentis aUserAgent. Returns:Task. Sends the request asynchronously.
tls_identity(identity_or_null)overrides the user-agent TLS identity for that request only. Passingnulldisables any user-agent identity for that request.ResponseRepresents an HTTP response.
response.status(),response.reason(),response.url()Parameters: none. Returns:
Numberforstatus, otherwiseString. Returns response metadata.response.content()Parameters: none. Returns:
BinaryStringor value. Returns the response body.response.headers()Parameters: none. Returns: header data. Returns response headers.
response.header(String name)Parameters:
nameis a header name. Returns:Stringornull. Returns the first matching response header.response.success()Parameters: none. Returns:
Boolean. Returns true for a successful HTTP status.response.json()Parameters: none. Returns: value. Decodes the response body as JSON.
response.expect_success()Parameters: none. Returns:
Response. Returns the response or throws for an unsuccessful status.response.to_Dict()Parameters: none. Returns:
Dict. Converts the response to a dictionary.
JS/BROWSER SYNCHRONOUS REQUEST SUPPORT
In the JS/Browser implementation, synchronous request methods do not perform real network requests. These methods return a Response with status 599 and reason Synchronous HTTP is unsupported on JS/Browser. Use the asynchronous methods in JS/Browser.
COPYRIGHT AND LICENCE
std/net/http is copyright Toby Inkster.
It is free software; you may redistribute it and/or modify it under the terms of either the Artistic License 1.0 or the GNU General Public License version 2.