#http-request #http-middleware #middleware #http

reqwest-middleware

Wrapper around reqwest to allow for client middleware chains

18 releases

0.4.0 Nov 11, 2024
0.3.2 Jun 28, 2024
0.2.5 Mar 15, 2024
0.2.4 Oct 24, 2023
0.1.2 Sep 28, 2021

#6 in HTTP client

Download history 217710/week @ 2024-08-24 208568/week @ 2024-08-31 211060/week @ 2024-09-07 191136/week @ 2024-09-14 219217/week @ 2024-09-21 238619/week @ 2024-09-28 236546/week @ 2024-10-05 238308/week @ 2024-10-12 255608/week @ 2024-10-19 243870/week @ 2024-10-26 234628/week @ 2024-11-02 243716/week @ 2024-11-09 246510/week @ 2024-11-16 208569/week @ 2024-11-23 264675/week @ 2024-11-30 244758/week @ 2024-12-07

1,008,429 downloads per month
Used in 441 crates (214 directly)

MIT/Apache

40KB
540 lines

reqwest-middleware

A crate implementing a wrapper around reqwest to allow for client middleware chains.

Crates.io Docs.rs CI Coverage Status

This crate provides functionality for building and running middleware but no middleware implementations. This repository also contains a couple of useful concrete middleware crates:

Note about browser support: automated tests targeting wasm are disabled. The crate may work with wasm but wasm support is unmaintained. PRs improving wasm are still welcome but you'd need to reintroduce the tests and get them passing before we'd merge it (see https://github.com/TrueLayer/reqwest-middleware/pull/105).

Overview

The reqwest-middleware client exposes the same interface as a plain reqwest client, but ClientBuilder exposes functionality to attach middleware:

# Cargo.toml
# ...
[dependencies]
reqwest = { version = "0.12", features = ["rustls-tls"] }
reqwest-middleware = "0.3"
reqwest-retry = "0.5"
reqwest-tracing = "0.5"
tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] }
use reqwest_middleware::{ClientBuilder, ClientWithMiddleware};
use reqwest_retry::{RetryTransientMiddleware, policies::ExponentialBackoff};
use reqwest_tracing::TracingMiddleware;

#[tokio::main]
async fn main() {
    // Retry up to 3 times with increasing intervals between attempts.
    let retry_policy = ExponentialBackoff::builder().build_with_max_retries(3);
    let client = ClientBuilder::new(reqwest::Client::new())
        // Trace HTTP requests. See the tracing crate to make use of these traces.
        .with(TracingMiddleware::default())
        // Retry failed requests.
        .with(RetryTransientMiddleware::new_with_policy(retry_policy))
        .build();
    run(client).await;
}

async fn run(client: ClientWithMiddleware) {
    client
        .get("https://truelayer.com")
        .header("foo", "bar")
        .send()
        .await
        .unwrap();
}

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Third-party middleware

The following third-party middleware use reqwest-middleware:

Dependencies

~4–17MB
~248K SLoC