#reqwest #http #middleware

reqwest-middleware

Wrapper around reqwest to allow for client middleware chains

12 releases

0.2.4 Oct 24, 2023
0.2.2 May 11, 2023
0.2.1 Mar 9, 2023
0.2.0 Nov 15, 2022
0.1.2 Sep 28, 2021

#7 in HTTP client

Download history 66683/week @ 2023-11-03 65761/week @ 2023-11-10 66147/week @ 2023-11-17 67675/week @ 2023-11-24 64836/week @ 2023-12-01 68909/week @ 2023-12-08 69974/week @ 2023-12-15 30950/week @ 2023-12-22 47348/week @ 2023-12-29 73067/week @ 2024-01-05 88046/week @ 2024-01-12 107431/week @ 2024-01-19 103856/week @ 2024-01-26 112881/week @ 2024-02-02 121441/week @ 2024-02-09 93763/week @ 2024-02-16

451,378 downloads per month
Used in 187 crates (135 directly)

MIT/Apache

29KB
446 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 targetting 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 = "0.11"
reqwest-middleware = "0.1.6"
reqwest-retry = "0.1.5"
reqwest-tracing = "0.2.3"
tokio = { version = "1.12.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 request-middleware:

Dependencies

~4–18MB
~262K SLoC