Module std::net::http::http_client

HTTP client compatibility module for outbound requests.

Provides basic outbound HTTP functionality for native Hew programs via import std::net::http::http_client;.

This compatibility module now carries the bounded v0.3 client slice: request, request_string, set_timeout, plus the legacy get / post helpers built on the same request path.

Response accessors: status(), body(), header(name), content_type(), headers() (all headers as Vec<(string, string)>), and free(). The _string convenience helpers (request_string, get_string, post_string) return Option<string> โ€” None on transport failure.

Examples

import std::net::http::http_client;

fn main() {
    match http_client.get_string("https://example.com") {
        Some(body) => println(body),
        None => println("request failed"),
    }

    let headers: Vec<(string, string)> = Vec::new();
    headers.push(("Accept", "application/json"));
    let resp = http_client.request("GET", "https://api.example.com/data", "", headers);
    println(resp.status());
    resp.free();
}

Contents

Functions

Function request

pub fn request(method: string, url: string, body: string, headers: Vec<(string, string)>) -> Response

Perform an HTTP request and return a Response handle.

Each header entry is a (name, value) pair, e.g. ("Accept", "application/json").

Examples

let headers: Vec<(string, string)> = Vec::new();
headers.push(("Accept", "application/json"));
let resp = http_client.request("GET", "https://example.com", "", headers);
println(resp.status());
resp.free();

Function request_string

pub fn request_string(method: string, url: string, body: string, headers: Vec<(string, string)>) -> Option<string>

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 None on transport or network failure. Non-2xx HTTP responses still return Some (the body may be empty); use http_client.request to inspect the status code.

Examples

let headers: Vec<(string, string)> = Vec::new();
match http_client.request_string("GET", "https://example.com", "", headers) {
    Some(body) => println(body),
    None => println("request failed"),
}

Function set_timeout

pub fn set_timeout(timeout_ms: i64)

Set the global timeout applied to subsequent outbound HTTP requests.

Pass 0 to disable the timeout.

Function last_error

pub fn last_error() -> string

Return the last HTTP client error observed on this thread.

Accessors that return null on allocation failure update this string. Successful accessor calls clear it.

Function get

pub fn get(url: string) -> Response

Perform an HTTP GET request and return a Response handle.

Examples

let resp = http_client.get("https://example.com");
println(resp.status());
resp.free();

Function post

pub fn post(url: string, content_type: string, body: string) -> Response

Perform an HTTP POST request and return a Response handle.

Examples

let resp = http_client.post("https://api.example.com/data", "application/json", "{}");
resp.free();

Function get_string

pub fn get_string(url: string) -> Option<string>

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 None on transport or network failure. Non-2xx HTTP responses still return Some (the body may be empty); use http_client.get to inspect the status code.

Examples

match http_client.get_string("https://example.com") {
    Some(body) => println(body),
    None => println("request failed"),
}

Function post_string

pub fn post_string(url: string, content_type: string, body: string) -> Option<string>

Perform an HTTP POST request and return the response body as a string.

Returns None on transport or network failure. Non-2xx HTTP responses still return Some (the body may be empty); use http_client.post to inspect the status code.

Examples

match http_client.post_string(
    "https://api.example.com/submit",
    "application/json",
    "{\"key\": \"value\"}",
) {
    Some(body) => println(body),
    None => println("request failed"),
}

Types

Struct Response

An opaque HTTP response handle.

Obtained from http_client.request(...), http_client.get(...), or http_client.post(...). Must be freed with free() when no longer needed.

Traits

Trait ResponseMethods

Methods available on an HTTP Response.

Methods

fn status(self: Self) -> i64

Return the HTTP status code (e.g. 200, 404). Returns -1 on error.

fn body(self: Self) -> string

Return a copy of the response body as a string.

fn header(self: Self, name: string) -> string

Look up a response header by name (case-insensitive).

Returns an empty string if the header is not present.

fn content_type(self: Self) -> string

Return the Content-Type response header.

Returns an empty string if not present.

fn headers(self: Self) -> Vec<(string, string)>

Return all response headers as a list of (name, value) pairs.

The returned vector is owned by the caller; Hew frees it automatically when it goes out of scope. Returns an empty vector if no headers were captured or the response is invalid.

fn free(self: Self)

Release the response resources.