#request #memory #middleware #header #response #body #recorder #client

httpclient

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

37 releases (16 breaking)

0.18.0 Jun 7, 2023
0.16.0 Mar 29, 2023
0.10.0 Dec 15, 2022
0.9.0 Nov 14, 2022
0.1.12 Mar 31, 2022

#56 in HTTP client

Download history 225/week @ 2023-05-29 359/week @ 2023-06-05 191/week @ 2023-06-12 364/week @ 2023-06-19 266/week @ 2023-06-26 202/week @ 2023-07-03 206/week @ 2023-07-10 199/week @ 2023-07-17 172/week @ 2023-07-24 82/week @ 2023-07-31 215/week @ 2023-08-07 244/week @ 2023-08-14 90/week @ 2023-08-21 130/week @ 2023-08-28 262/week @ 2023-09-04 99/week @ 2023-09-11

653 downloads per month
Used in 24 crates

MIT license

68KB
1.5K 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 send_awaiting_body which awaits both the request and the response body, which greatly simplifies the scenario where you want to return the request body even in error cases.

Note: httpclient is under active development and is alpha quality software. While we make effort not to change public APIs, we do not currently provide stability guarantees.

Examples

#[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::RecorderMiddleware::new())
        ;
    let res = client.get("https://www.jsonip.com/")
        .header("secret", "foo")
        .await
        .unwrap();
    // Note the default API (identical to reqwest) requires `await`ing the response body.
    let res = res.text().await.unwrap();
    
    let res = client.get("https://www.jsonip.com/")
        .header("secret", "foo")
        .send_awaiting_body()
        .await
        .unwrap();
    // By using `send_awaiting_body`, the response is loaded into memory, and we don't have to `await` it.
    let res = res.text().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–20MB
~336K SLoC