Repository navigation
HTTP
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.
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.
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.
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
Begin here
Values and expressions
- Language basics
- Variables and scope
- Numbers and logic
- Strings and paths
- Arrays
- Hash maps
- Ranges and iteration
- Control flow
- Match and type guards
Functions and types
- Functions and calls
- Closures and function types
- Methods and properties
- Structs
- Enums
- Named types and unions
- Conversions
- Types and properties
Shell and modules
- Pipelines
- Pipeline splits
- Pipeline formats
- Commands and exit status
- Redirection
- Environment and exported variables
- Modules and visibility
- Nested modules and directory packages
- Conditional imports and declarations
- Errors and cleanup
- Supervised processes and terminals
Library guides
- Standard library
- The implicit prelude
- Bytes, files, readers, and writers
- Explicit item streams
- JSON documents and typed mapping
- Sockets, DNS, and TLS
- HTTP clients
- HTTP servers
- Framing, server-sent events, and JSON-RPC
- Terminal colors, styles, and geometry
- Build an application console
Interactive use