Expand description
§htrio
The htrio crate provides a convenient, higher-level HTTP
Client.
It handles many of the things that most people just expect an HTTP client to do for them.
- Async and blocking Clients
- Plain bodies, JSON, urlencoded, multipart
- Customizable redirect policy
- HTTP Proxies
- Uses TLS by default
- Cookies
The htrio::Client is asynchronous (requiring Tokio). For
applications wishing to only make a few HTTP requests, the
htrio::blocking API may be more convenient.
Additional learning resources include:
§Commercial Support
For private advice, support, reviews, access to the maintainer, and the like, reach out for commercial support.
§Making a GET request
For a single request, you can use the get shortcut method.
let body = htrio::get("https://www.rust-lang.org")
.await?
.text()
.await?;
println!("body = {body:?}");NOTE: If you plan to perform multiple requests, it is best to create a
Client and reuse it, taking advantage of keep-alive connection
pooling.
§Making POST requests (or setting request bodies)
There are several ways you can set the body of a request. The basic one is
by using the body() method of a RequestBuilder. This lets you set the
exact raw bytes of what the body should be. It accepts various types,
including String and Vec<u8>. If you wish to pass a custom
type, you can use the htrio::Body constructors.
let client = htrio::Client::new();
let res = client.post("http://httpbin.org/post")
.body("the exact body that is sent")
.send()
.await?;§Forms
It’s very common to want to send form data in a request body. This can be done with any type that can be serialized into form data.
This can be an array of tuples, or a HashMap, or a custom type that
implements Serialize.
The feature form is required.
// This will POST a body of `foo=bar&baz=quux`
let params = [("foo", "bar"), ("baz", "quux")];
let client = htrio::Client::new();
let res = client.post("http://httpbin.org/post")
.form(¶ms)
.send()
.await?;§JSON
There is also a json method helper on the RequestBuilder that works in
a similar fashion the form method. It can take any value that can be
serialized into JSON.
The feature json is required.
// This will POST a body of `{"lang":"rust","body":"json"}`
let mut map = HashMap::new();
map.insert("lang", "rust");
map.insert("body", "json");
let client = htrio::Client::new();
let res = client.post("http://httpbin.org/post")
.json(&map)
.send()
.await?;§Redirect Policies
By default, a Client will automatically handle HTTP redirects, having a
maximum redirect chain of 10 hops. To customize this behavior, a
redirect::Policy can be used with a ClientBuilder.
§Cookies
The automatic storing and sending of session cookies can be enabled with
the cookie_store method on ClientBuilder.
§Proxies
NOTE: System proxies are enabled by default.
System proxies look in environment variables to set HTTP or HTTPS proxies.
HTTP_PROXY or http_proxy provide HTTP proxies for HTTP connections while
HTTPS_PROXY or https_proxy provide HTTPS proxies for HTTPS connections.
ALL_PROXY or all_proxy provide proxies for both HTTP and HTTPS connections.
If both the all proxy and HTTP or HTTPS proxy variables are set the more specific
HTTP or HTTPS proxies take precedence.
These can be overwritten by adding a Proxy to ClientBuilder
i.e. let proxy = htrio::Proxy::http("https://secure.example")?;
or disabled by calling ClientBuilder::no_proxy().
socks feature is required if you have configured socks proxy like this:
export https_proxy=socks5://127.0.0.1:1086§TLS
A Client will use transport layer security (TLS) by default to connect to
HTTPS destinations.
- Additional server certificates can be configured on a
ClientBuilderwith theCertificatetype. - Client certificates can be added to a
ClientBuilderwith theIdentitytype. - Various parts of TLS can also be configured or even disabled on the
ClientBuilder.
See more details in the tls module.
§HTTP versions and transports
HTTP/1.0 and HTTP/1.1 use Hyper. The default HTTPS TCP connection can
negotiate HTTP/2 with ALPN. With http3, authenticated Alt-Svc advertisements
can select an HTTP/3 connection for a later request to the same origin.
Each redirect hop makes its own transport decision.
HTTP/3 uses the local htrio-h3 and htrio-h3-quinn crates. It supports
direct QUIC and SOCKS5 UDP ASSOCIATE. SOCKS5 requires socks. Ordinary HTTP
CONNECT proxies continue to use TCP. Proxy selection never falls back to a
direct connection when the selected tunnel cannot be established.
The http3_prior_knowledge builder method and an explicit HTTP/3 request
require an HTTP/3 server and never fall back to TCP. See ClientBuilder for
alternative service discovery, connection budgets, and TLS controls.
This crate supports native targets only; WASM is intentionally unsupported.
§Optional Features
The following are a list of Cargo features that can be enabled or disabled:
- http2 (enabled by default): Enables HTTP/2 support.
- http3 (enabled by default): Enables HTTP/3 and Alt-Svc discovery.
- default-tls (enabled by default): Provides TLS support to connect over HTTPS.
- rustls (enabled by default): Enables TLS functionality provided by
rustls. - rustls-tls: Compatibility alias for
rustls. - rustls-no-provider: Enables TLS provided by
rustlswithout specifying a crypto provider. - native-tls: Enables TLS functionality provided by
native-tls. - native-tls-vendored: Enables the
vendoredfeature ofnative-tls. - native-tls-no-alpn: Enables
native-tlswithout itsalpnfeature. - native-tls-vendored-no-alpn: Enables
native-tls-vendoredwithout itsalpnfeature. - blocking: Provides the blocking client API.
- charset (enabled by default): Improved support for decoding text.
- cookies (enabled by default): Provides cookie session support.
- gzip (enabled by default): Provides response body gzip decompression.
- brotli (enabled by default): Provides response body brotli decompression.
- zstd (enabled by default): Provides response body zstd decompression.
- deflate (enabled by default): Provides response body deflate decompression.
- query (enabled by default): Provides query parameter serialization.
- form (enabled by default): Provides form data serialization.
- json (enabled by default): Provides serialization and deserialization for JSON bodies.
- multipart (enabled by default): Provides functionality for multipart forms.
- stream (enabled by default): Adds support for
futures::Stream. - socks: Provides SOCKS proxies, including SOCKS5 UDP for HTTP/3.
- hickory-dns: Enables a hickory-dns async resolver instead of default
threadpool using
getaddrinfo. - system-proxy (enabled by default): Use Windows and macOS system proxy settings automatically.
HTTP/3 is enabled with the http3 Cargo feature and requires no custom compiler flags.
Cookie storage requires runtime opt-in. Proxy selection honors configured
rules and environment/system defaults; ClientBuilder::no_proxy disables
proxy use. Disable automatic discovery with alt_svc(false).
Re-exports§
pub use tls::Certificate;__tlsand not (WebAssembly and (target_os=unknownor bare-metal))pub use tls::Identity;__tlsand not (WebAssembly and (target_os=unknownor bare-metal))
Modules§
- alt_svc
http3 - Bounded, origin-scoped HTTP/3 alternative service cache (RFC 7838).
- blocking
blockingand not (WebAssembly and (target_os=unknownor bare-metal)) - A blocking Client API.
- cookie
cookiesand not (WebAssembly and (target_os=unknownor bare-metal)) - HTTP Cookies
- dns
Not (WebAssembly and ( target_os=unknownor bare-metal)) - DNS resolution
- header
- HTTP header types
- http3
http3 - HTTP/3 transport metadata and native QUIC configuration.
- multipart
multipartand not (WebAssembly and (target_os=unknownor bare-metal)) - multipart/form-data
- redirect
Not (WebAssembly and ( target_os=unknownor bare-metal)) - Redirect Handling
- retry
Not (WebAssembly and ( target_os=unknownor bare-metal)) - Retry requests
- tls
__tlsand not (WebAssembly and (target_os=unknownor bare-metal)) - TLS configuration and types
Structs§
- Body
Not (WebAssembly and ( target_os=unknownor bare-metal)) - An asynchronous request body.
- Client
Not (WebAssembly and ( target_os=unknownor bare-metal)) - An asynchronous
Clientto make Requests with. - Client
Builder Not (WebAssembly and ( target_os=unknownor bare-metal)) - A
ClientBuildercan be used to create aClientwith custom configuration. - Error
- The Errors that may occur when processing a
Request. - Method
- The Request Method (VERB)
- NoProxy
Not (WebAssembly and ( target_os=unknownor bare-metal)) - A configuration for filtering out requests that shouldn’t be proxied
- Proxy
Not (WebAssembly and ( target_os=unknownor bare-metal)) - Configuration of a proxy that a
Clientshould pass requests to. - Request
Not (WebAssembly and ( target_os=unknownor bare-metal)) - A request which can be executed with
Client::execute(). - Request
Builder Not (WebAssembly and ( target_os=unknownor bare-metal)) - A builder to construct the properties of a
Request. - Response
Not (WebAssembly and ( target_os=unknownor bare-metal)) - A Response to a submitted
Request. - Status
Code - An HTTP status code (
status-codein RFC 9110 et al.). - Upgraded
Not (WebAssembly and ( target_os=unknownor bare-metal)) - An upgraded HTTP connection.
- Url
- A parsed URL record.
- Version
- Represents a version of the HTTP spec.
Traits§
- IntoUrl
- A trait to try to convert some type into a
Url. - Response
Builder Ext - Extension trait for http::response::Builder objects
- Unix
Socket Provider docsrsand Unix and not (WebAssembly and (target_os=unknownor bare-metal)) - A provider for Unix Domain Socket paths.
Functions§
- get
- Shortcut method to quickly make a
GETrequest.
Type Aliases§
- Result
- A
Resultalias where theErrcase ishtrio::Error.