#http-request #middleware #exponential-backoff #client #request-response #logging #response-body

httpclient

HTTP client with middleware. Middleware provides composable support for record/replay, logging, exponential backoff, and more.

51 releases (19 breaking)

0.21.3 Jan 22, 2024
0.20.6 Jan 1, 2024
0.20.2 Dec 25, 2023
0.19.0 Nov 29, 2023
0.1.12 Mar 31, 2022

#46 in HTTP client

Download history 79/week @ 2023-12-19 74/week @ 2023-12-26 55/week @ 2024-01-02 51/week @ 2024-01-09 36/week @ 2024-01-16 16/week @ 2024-01-23 103/week @ 2024-01-30 52/week @ 2024-02-06 37/week @ 2024-02-13 82/week @ 2024-02-20 189/week @ 2024-02-27 90/week @ 2024-03-05 369/week @ 2024-03-12 1593/week @ 2024-03-19 75/week @ 2024-03-26 203/week @ 2024-04-02

2,286 downloads per month
Used in 28 crates

MIT license

76KB
2K SLoC

GitHub Contributors Stars Build Status Downloads Crates.io

HttpClient

httpclient is a user-friendly http client in Rust. Where possible, it closely mimics the reqwest API. Why build a new http client?

  • httpclient::{Request, Response} objects are serde-serializable, which enables record/replay functionality. See the example below to see it in action.
  • httpclient provides an API for user-extensible middleware. Built-in middleware includes redirect, retry, logging, and record/replay.
  • httpclient provides a built-in Error type that can return the Http request, which includes the status code, headers, and response body.
  • httpclient provides convenience methods that reqwest does not support. The most important is the IntoFuture implementation, which awaits both the request and the response body, which simplifies the scenario where you want to return the request body even in error cases.

Oauth2

For Oauth2, use Oauth2Flow and the Oauth2 middleware from httpclient_oauth2.

Note on Http 1.0

http was recently upgraded to 1.0. However, hyper_rustls still depends on 0.2.x. We are waiting for hyper_rustls before bumping our own dependency.

#[tokio::main]
async fn main() {
    let mut client = httpclient::Client::new()
        // With this middleware, the script will only make the request once. After that, it replays from the filesystem
        // and does not hit the remote server. The middleware has different modes to ignore recordings (to force refresh)
        // and to prevent new requests (for running a test suite).
        // The recordings are sanitized to hide secrets.
        .with_middleware(httpclient::middleware::Recorder::new())
        ;
    let res = client.get("https://www.jsonip.com/")
        .header("secret", "foo")
        .await
        .unwrap();
    let res = res.text().unwrap();
    
    let res = client.get("https://www.jsonip.com/")
        .header("secret", "foo")
        .send()
        .await
        .unwrap();
    // By using `send()`, we now can separately await the request body.
    let res = res.text().await.unwrap();
}

Roadmap

  • Hide secrets in Recorder. Hash & Eq checks for requests must respect hidden values.
  • Ensure it builds on wasm32-unknown-unknown
  • Sanitize "sessid" in json

Dependencies

~9–23MB
~371K SLoC