#reqwest #http #middleware

reqwest-middleware

Wrapper around reqwest to allow for client middleware chains

7 releases

0.1.6 Apr 21, 2022
0.1.5 Feb 21, 2022
0.1.4 Jan 24, 2022
0.1.3 Dec 6, 2021
0.1.0 Aug 12, 2021

#24 in HTTP client

Download history 7776/week @ 2022-04-27 7868/week @ 2022-05-04 7892/week @ 2022-05-11 7447/week @ 2022-05-18 7694/week @ 2022-05-25 11586/week @ 2022-06-01 9450/week @ 2022-06-08 11219/week @ 2022-06-15 9418/week @ 2022-06-22 11959/week @ 2022-06-29 11305/week @ 2022-07-06 11191/week @ 2022-07-13 12541/week @ 2022-07-20 12346/week @ 2022-07-27 13522/week @ 2022-08-03 13468/week @ 2022-08-10

54,107 downloads per month
Used in 67 crates (54 directly)

MIT/Apache

15KB
282 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:

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.1"
reqwest-retry = "0.1.1"
reqwest-tracing = "0.2.0"
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)
        // 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.

Dependencies

~6–14MB
~293K SLoC