Expand description
This project’s goal is to provide a lightweight and simple HTTP client for the Rust ecosystem. The intended use is for projects that have HTTP needs where performance is not critical or when HTTP is not the main purpose of the application. Note that the project still tries to perform well and avoid allocation where possible, but stays away from Rust’s asynchronous stack to provide a crate that’s as small as possible. Features are provided behind feature flags when possible to allow users to get just what they need.
Check out the repository for more information and examples.
§Quick start
let obj = json!({
"hello": "world",
});
let resp = attohttpc::post("https://my-api.org/do/something")
.header("X-My-Header", "foo") // set a header for the request
.param("qux", "baz") // set a query parameter
.json(&obj)? // set the request body (json feature required)
.send()?; // send the request
// Check if the status is a 2XX code.
if resp.is_success() {
// Consume the response body as text and print it.
println!("{}", resp.text()?);
}§Features
basic-authsupport for basic authcharsetssupport for decoding more text encodings than just UTF-8compresssupport for decompressing response bodies usingminiz_oxide(default)compress-zlibsupport for decompressing response bodies usingzlibinstead ofminiz_oxide(see flate2 backends)compress-zlib-ngsupport for decompressing response bodies usingzlib-nginstead ofminiz_oxide(see flate2 backends)jsonsupport for serialization and deserializationformsupport for url encoded forms (does not include support for multipart)multipart-formsupport for multipart forms (does not include support for url encoding)tls-nativesupport for tls connections using thenative-tlscrate (default)tls-native-vendoredactivate thevendoredfeature ofnative-tlstls-rustls-webpki-rootssupport for TLS connections usingrustlsinstead ofnative-tlswith Web PKI rootstls-rustls-native-rootssupport for TLS connections usingrustlswith root certificates loaded from therustls-native-certscrate
§Activating a feature
To activate a feature, specify it in your Cargo.toml file like so
attohttpc = { version = "...", features = ["json", "form", ...] }Re-exports§
pub use crate::charsets::Charset;
Modules§
- body
- Contains types to describe request bodies
- charsets
- This module is a clean re-export of the
encoding_rscrate. You can probably find the charset you need in here. - header
- This module is a re-export of the
httpcrate’sheadermodule.
Structs§
- Error
- A type that contains all the errors that can possibly occur while accessing an HTTP server.
- Method
- The Request Method (VERB)
- Multipart
- A multipart form created using
MultipartBuilder. - Multipart
Builder - A builder for creating a
Multipartbody. - Multipart
File - A file to be uploaded as part of a multipart form.
- Prepared
Request - Represents a request that’s ready to be sent. You can inspect this object for information about the request.
- Proxy
Settings - Contains proxy settings and utilities to find which proxy to use for a given URL.
- Proxy
Settings Builder - Utility to build ProxySettings easily.
- Request
Builder RequestBuilderis the main way of building requests.- Request
Inspector - Allows to inspect the properties of a request before preparing it.
- Response
Responserepresents a response returned by a server.- Response
Reader - The
ResponseReaderis used to read the body of a response. - Session
Sessionis a type that can carry settings over multiple requests. The settings applied to theSessionare applied to every request created from thisSession.- Status
Code - An HTTP status code (
status-codein RFC 9110 et al.). - Text
Reader TextReaderconverts bytes in a specific charset to bytes in UTF-8.
Enums§
- Error
Kind - Common errors that can occur during HTTP requests.
- Invalid
Response Kind - Errors than can occur while parsing the response from the server.
Functions§
- delete
- Create a new
RequestBuilderwith the DELETE method. - get
- Create a new
RequestBuilderwith the GET method. - head
- Create a new
RequestBuilderwith the HEAD method. - options
- Create a new
RequestBuilderwith the OPTIONS method. - patch
- Create a new
RequestBuilderwith the PATCH method. - post
- Create a new
RequestBuilderwith the POST method. - put
- Create a new
RequestBuilderwith the PUT method. - trace
- Create a new
RequestBuilderwith the TRACE method.
Type Aliases§
- Result
- Wrapper for the
Resulttype with anError.