Module std.net.http.client
HTTP client module for outbound requests.
Provides basic outbound HTTP functionality for native Hew programs via
import std.net.http.client;.
Requests and body decoding return checked errors with operation detail.
Response accessors: status(), body(), header(name), content_type(),
headers() (all headers as Vec<(string, string)>), and close().
The _string convenience helpers (request_string, get_string,
post_string) return checked results, preserving transport and decoding errors.
Examples
import std.net.http.client;
fn main() {
match client.get_string("https://example.com") {
.Ok(body) => println(body),
.Err(err) => println(f"request failed: {err}"),
}
var headers: Vec<(string, string)> = Vec.new();
headers.push(("Accept", "application/json"));
let resp = client.request("GET", "https://api.example.com/data", "", headers).expect("request succeeds");
println(resp.status());
resp.close();
}
Contents
- fn request
- fn request_string
- fn set_timeout
- fn get
- fn post
- fn get_string
- fn post_string
- struct Response
- enum HttpError
- inherent methods
Functions
Function request
pub fn request(method: string, url: string, body: string, headers: Vec<(string, string)>) -> Response fails HttpError
Perform an HTTP request and return a Response handle.
Each header entry is a (name, value) pair, e.g. ("Accept", "application/json").
Examples
import std.net.http.client;
fn main() {
var headers: Vec<(string, string)> = Vec.new();
headers.push(("Accept", "application/json"));
let resp = client.request("GET", "https://example.com", "", headers).expect("request succeeds");
println(resp.status());
resp.close();
}
Function request_string
pub fn request_string(method: string, url: string, body: string, headers: Vec<(string, string)>) -> string fails HttpError
Perform an HTTP request and return the response body as a string.
Each header entry is a (name, value) pair, e.g. ("Accept", "application/json").
Returns an error on transport or network failure. Non-2xx HTTP responses
still return Ok (the body may be empty); use client.request
to inspect the status code.
Examples
import std.net.http.client;
fn main() {
var headers: Vec<(string, string)> = Vec.new();
match client.request_string("GET", "https://example.com", "", headers) {
.Ok(body) => println(body),
.Err(err) => println(f"request failed: {err}"),
}
}
Function set_timeout
pub fn set_timeout(timeout: duration) -> () fails net.NetError
Set the global outbound-request timeout, rejecting a value the C ABI cannot represent rather than narrowing it into a different deadline.
Function get
pub fn get(url: string) -> Response fails HttpError
Perform an HTTP GET request and return a Response handle.
Examples
import std.net.http.client;
fn main() {
let resp = client.get("https://example.com").expect("request succeeds");
println(resp.status());
resp.close();
}
Function post
pub fn post(url: string, content_type: string, body: string) -> Response fails HttpError
Perform an HTTP POST request and return a Response handle.
Examples
import std.net.http.client;
fn main() {
let resp = client.post("https://api.example.com/data", "application/json", "{}").expect("request succeeds");
resp.close();
}
Function get_string
pub fn get_string(url: string) -> string fails HttpError
Perform an HTTP GET request and return the response body as a string.
This is a convenience function that combines the request and body extraction in a single call.
Returns an error on transport or network failure. Non-2xx HTTP responses
still return Ok (the body may be empty); use client.get
to inspect the status code.
Examples
import std.net.http.client;
fn main() {
match client.get_string("https://example.com") {
.Ok(body) => println(body),
.Err(err) => println(f"request failed: {err}"),
}
}
Function post_string
pub fn post_string(url: string, content_type: string, body: string) -> string fails HttpError
Perform an HTTP POST request and return the response body as a string.
Returns an error on transport or network failure. Non-2xx HTTP responses
still return Ok (the body may be empty); use client.post
to inspect the status code.
Examples
import std.net.http.client;
fn main() {
match client.post_string(
"https://api.example.com/submit",
"application/json",
"{\"key\": \"value\"}",
) {
.Ok(body) => println(body),
.Err(err) => println(f"request failed: {err}"),
}
}
Types
Struct Response
An opaque HTTP response handle.
Obtained from client.request(...), client.get(...), or
client.post(...). Responses close automatically at scope exit; call
close() to release one early.
Methods
fn status(self) -> i64
Return the HTTP status code (e.g. 200, 404). The response has a valid HTTP status.
fn header(self, name: string) -> Option<string> fails HttpError
Look up a response header by name (case-insensitive).
Returns None if the header is not present.
Enum HttpError
A failed HTTP operation and its transport, protocol or decoding detail.
Variants
Inherent methods
impl Response
fn close(consume self)
Release this response early.