Reqwest: Rust HTTP Client Library

Reqwest is a high-level HTTP client library that simplifies the HTTP request handling process and provides many commonly used features:

  • Support for both asynchronous and blocking APIs
  • Handling various types of request bodies: plain text, JSON, URL-encoded forms, multipart forms
  • Customizable redirect policies
  • HTTP proxy support
  • TLS encryption enabled by default
  • Cookie management

Basic Usage

Making a GET Request

For single requests, you can use the get convenience method:

let body = reqwest::get("https://www.rust-lang.org")
    .await?
    .text()
    .await?;

println!("body = {body:?}");

Note: If you plan to make multiple requests, it is better to create a Client and reuse it to take advantage of connection pooling.

let client = reqwest::Client::new();
let res = client.get("https://www.rust-lang.org")
    .send()
    .await?;

Making a POST Request

You can set the request body using the body() method:

let client = reqwest::Client::new();
let res = client.post("http://httpbin.org/post")
    .body("the exact body that is sent")
    .send()
    .await?;

Form Data

Sending form data is a common requirement. You can use any type that can be serialized into form data:

// This will send a POST request with a body of `foo=bar&baz=quux`
let params = [("foo", "bar"), ("baz", "quux")];
let client = reqwest::Client::new();
let res = client.post("http://httpbin.org/post")
    .form(&params)
    .send()
    .await?;

JSON Data

You can easily send JSON data using the json method (requires the json feature):

// This will send a POST request with a body of `{"lang":"rust","body":"json"}`
let mut map = HashMap::new();
map.insert("lang", "rust");
map.insert("body", "json");

let client = reqwest::Client::new();
let res = client.post("http://httpbin.org/post")
    .json(&map)
    .send()
    .await?;

Response Handling

Responses can be handled in various ways:

let res = client.get("https://www.rust-lang.org").send().await?;

// Get the status code
let status = res.status();

// Get response headers
let content_type = res.headers().get("content-type").unwrap();

// Read the response body as text
let body = res.text().await?;

// Or parse it as JSON
let json: serde_json::Value = res.json().await?;

Advanced Features

Redirect Policy

By default, the client will automatically handle HTTP redirects, following up to 10 jumps. You can customize this behavior using ClientBuilder:

let custom_client = reqwest::Client::builder()
    .redirect(reqwest::redirect::Policy::none()) // Disable redirects
    .build()?;

You can enable automatic storage and sending of session cookies via ClientBuilder:

let client = reqwest::Client::builder()
    .cookie_store(true)
    .build()?;

Proxy Settings

System proxies are enabled by default, which will look for HTTP or HTTPS proxy settings in environment variables:

  • HTTP_PROXY or http_proxy: proxy for HTTP connections
  • HTTPS_PROXY or https_proxy: proxy for HTTPS connections
  • ALL_PROXY or all_proxy: proxy for both types of connections

You can also explicitly set a proxy via code:

let proxy = reqwest::Proxy::http("https://secure.example")?;
let client = reqwest::Client::builder()
    .proxy(proxy)
    .build()?;

// Or disable proxies
let client = reqwest::Client::builder()
    .no_proxy()
    .build()?;

TLS Configuration

The client uses TLS by default to connect to HTTPS targets:

// Add additional server certificates
let cert = reqwest::Certificate::from_pem(&cert_bytes)?;
let client = reqwest::Client::builder()
    .add_root_certificate(cert)
    .build()?;

// Configure client certificates
let identity = reqwest::Identity::from_pkcs12_der(&pkcs12_der, "password")?;
let client = reqwest::Client::builder()
    .identity(identity)
    .build()?;

Timeout Settings

You can configure timeout durations for requests:

let client = reqwest::Client::builder()
    .timeout(std::time::Duration::from_secs(10))
    .build()?;

Optional Features

Reqwest provides various optional features that can be enabled or disabled via Cargo features:

  • http2 (enabled by default): support for HTTP/2
  • default-tls (enabled by default): provides TLS support for HTTPS
  • rustls-tls: provides TLS functionality using rustls
  • blocking: provides a blocking client API
  • json: provides JSON serialization and deserialization functionality
  • multipart: provides multipart form functionality
  • cookies: provides cookie session support
  • gzip, brotli, deflate, zstd: support for various response body decompression
  • socks: provides SOCKS5 proxy support

Blocking API

When asynchronous operations are not needed, you can use the blocking API (requires the blocking feature):

let body = reqwest::blocking::get("https://www.rust-lang.org")?.text()?;

let client = reqwest::blocking::Client::new();
let res = client.post("http://httpbin.org/post")
    .json(&map)
    .send()?;