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>;
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>;
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>;
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.
type struct Name { ... }
type enum Name { ... }
type trait Name { ... }
type union Name { ... }Begins a type declaration; the keyword that follows says which kind. On its own, type Name = Existing; declares an alias.
type struct Name {
field: Type;
method(&this) -> Ret { ... }
}A value type with named fields and optional methods. Structs live on the stack and are passed by value. Fields are public by default and may carry = default values; methods take their receiver as &this, mut &this, or this.
type struct Client {}HTTP client: simple one-shot requests over plain TCP.
static name(param: Type) -> Ret { ... }A method that belongs to the type rather than an instance, called as Type::name(..). For structs, static new(..) returning a struct literal is the idiomatic constructor.
static Client::new() -> Clientasync function name(param: Type) -> T { ... }Compiles the function into a stackless state machine. Calling it builds a future whose output is the declared return type; nothing runs until that future is polled. Allowed on free functions, methods, and trait methods.
language referenceasync Client::get(&this, addr: SocketAddr, path: Str) -> Result<Response, IoError>GET addr/path. Sets Host to addr's address:port.
&this
mut &this
thisThe receiver of a method. Its first parameter declares how the method takes it: &this borrows read-only, mut &this borrows exclusively, and this / mut this move the value in and consume it. In the body, this.field reaches the members.
(parameter) addr: SocketAddr(parameter) path: Strtype struct Str {
bytes: u8*;
length: u64;
}Str: a borrowed UTF-8 string slice.
std::collections::strtype struct Response {
status: StatusCode;
version: String;
headers: Headers;
body: Array<u8>;
}HTTP response.
std::net::http::responsetype struct IoError {
kind: IoErrorKind;
os_code: i32;
}async Client::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.
(parameter) content_type: Str(parameter) body: Array<u8>type struct Array<T, A = GlobalAlloc> where A: Allocator {
ptr: T*;
length: i64;
capacity: i64;
alloc: A;
}Array<T, A>: growable, heap-backed, contiguous sequence.
std::collections::arrayimport a::b;
import a::b::{ X, Y };
import a::b as c;
import a::b::*;Brings a module, or selected items from it, into scope. One path per declaration: use the brace form for several items, as to alias, and * for everything public (prefer the brace form; wildcards invite name collisions).
mut name: Type = value;A mutable binding, reassignable after declaration - mutability is opt-in, const is the default. At module level it declares mutable global state. Before & it marks an exclusive reference or receiver instead.
mut c: Clientstatic Client::new() -> Clientconst NAME: Type = value;At module level, a true compile-time constant, conventionally SCREAMING_SNAKE_CASE. (mut at module level declares mutable global state instead.)
const respmut c: Clientasync Client::get(&this, addr: SocketAddr, path: Str) -> Result<Response, IoError>GET addr/path. Sets Host to addr's address:port.
static Str::new(text: string) -> StrConstruct from a NUL-terminated C-style string literal. The caller certifies that text is valid UTF-8 and outlives this view. The NUL terminator is not included in the view.
async Client::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.
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.
function name<T>(param: Type) -> Ret { ... }Declares a function. Parameters are written name: Type and are never inferred; the return type follows -> and defaults to void. Declaration order does not matter: every signature is collected before any body is checked, so functions may call each other freely and recurse.
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.
(parameter) req: Requesttype struct Request {
method: Method;
path: String;
version: String;
headers: Headers;
body: Array<u8>;
params: HashMap<String, String>;
}HTTP request.
std::net::http::request