Skip to content
Chloë Strainge edited this page Oct 14, 2026 · 1 revision

HTTP clients

std::net::http provides clients, requests, responses, and owned body readers. A Client stores a base URL and request policy. A Request adds method, path, headers, query pairs, body, and optional timeout settings.

Fetch a typed response

This example defines a reusable function. Calling it performs network I/O; the URL is intentionally supplied by the caller.

import std::net::http
struct BuildInfo { pub version: String, pub ready: Boolean }
fn fetch_build($base: String): BuildInfo | ErrorValue
{
    let $client = std::net::http::Client(base_url: $base)
    try
    {
        let $response = $client.get "/build"
        try
        {
            $response.require_success
            $response.read_as BuildInfo
        }
        finally { $response.close }
    }
    finally { $client.close }
}

get, post, put, and delete are conveniences; send accepts a Request. Query parameters are pairs, so repeated parameter names can be represented. Headers support case-insensitive lookup and multiple values.

Status and failures

Receiving an HTTP response is different from accepting its status. require_success turns an unsuccessful status into an error. Without it, inspect status and handle the response according to the application's policy. Transport, decoding, timeout, and status errors are separate from Unix ExecResult.

A response body has one consumer. Read it as bytes, text, an exact JSON document, a native JSON value, or an application type. take_body transfers a Reader for incremental processing. Do not independently read the same body twice.

Uploads and streaming

Body constructors cover text, JSON, Bytes, Readers, and byte sources. Streaming uploads have an explicit completion/closing lifecycle. Client settings include connection and idle deadlines; individual requests can impose a deadline too.

Error decoders can produce application-specific error values from bounded response diagnostics. Keep credentials in environment/configuration and use helpers such as bearer when constructing authentication headers.


Related: JSON · HTTP servers · Errors and cleanup

Clone this wiki locally