Module std::net::http::http_client

HTTP client for making outbound requests.

Provides functions for performing HTTP GET and POST requests and reading the response.

Examples

import std::net::http;

fn main() {
    let body = http_client.get_string("https://example.com");
    println(body);

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

Contents

Functions

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) -> 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.

Examples

let body = http_client.get_string("https://example.com");
println(body);

Function post_string

pub fn post_string(url: String, content_type: String, body: String) -> String

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

Returns an empty string on network error.

Examples

let body = http_client.post_string(
    "https://api.example.com/submit",
    "application/json",
    "{\"key\": \"value\"}",
);
println(body);

Types

Struct Response

An opaque HTTP response handle.

Obtained from http_client.get(url) 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: Response) -> i32 fn body(self: Response) -> String fn header(self: Response, name: String) -> String fn content_type(self: Response) -> String fn free(self: Response)