Skip to content
CryoCryo home
Stdlibnet::http

client

import std::net::http::client; · source

Types

Client

type struct Client {
    static new() -> Client;
    async get(&this, addr: SocketAddr, path: Str) -> Result<Response, IoError>;
    async post(&this, addr: SocketAddr, path: Str, content_type: Str, body: Array<u8>) -> Result<Response, IoError>;
}
import std::net::http::client;

mut c: Client = Client::new();
const resp = ex.block_on(c.get(addr, Str::new("/health")));

get and post are one-shot: connect, send, read the response, close. The free send sends a request you built yourself. The Host header is filled in from the address automatically, and Connection: close is set so the server does not hold the connection for keep-alive.

For HTTPS, net::https wraps the same machinery with TLS underneath.

Constructors

static new() -> Client;

Methods

async get(&this, addr: SocketAddr, path: Str) -> Result<Response, IoError>;

GET addr/path. Sets Host to addr's address:port.

async post(&this, addr: SocketAddr, path: Str, content_type: Str, body: Array<u8>) -> Result<Response, IoError>;

POST addr/path with body bytes. Sets Host and passes the supplied Content-Type through unchanged; if the caller didn't set one, the server may fall back to octet-stream.

Free Functions

async function send(addr: SocketAddr, req: Request) -> Result<Response, IoError>;

Open a connection, send the request, read one response, close. Emits Connection: close (RFC 7230 section 6.1) so the server knows this is a one-shot exchange and can free its half of the socket as soon as the response is written. Without this header, servers that default to HTTP/1.1 keep-alive will sit holding the connection open until their idle timeout fires.

Client::get and Client::post are conveniences over this, for when you do not need to build the Request yourself.