10 unstable releases (3 breaking)

0.4.1 Dec 4, 2024
0.4.0 Sep 12, 2024
0.3.2 May 5, 2024
0.3.0 Apr 30, 2024
0.1.2 Apr 19, 2024

#306 in HTTP client

Download history 42/week @ 2024-08-26 191/week @ 2024-09-09 60/week @ 2024-09-16 35/week @ 2024-09-23 42/week @ 2024-09-30 7/week @ 2024-10-07 4/week @ 2024-10-14 5/week @ 2024-10-21 2/week @ 2024-10-28 1/week @ 2024-11-04 22/week @ 2024-11-18 168/week @ 2024-12-02 24/week @ 2024-12-09

214 downloads per month
Used in tower-http-client

MIT/Apache

16KB
259 lines

tower-reqwest

tests crates.io Documentation MIT/Apache-2 licensed

This library provides adapters to use reqwest client with the tower_http layers.

Example

use http::{header::USER_AGENT, HeaderValue};
use http_body_util::BodyExt;
use serde_json::Value;
use tower::{ServiceBuilder, Service};
use tower_http::ServiceBuilderExt;
use tower_reqwest::HttpClientLayer;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let mut client = ServiceBuilder::new()
        // Add some layers.
        .override_request_header(USER_AGENT, HeaderValue::from_static("tower-reqwest"))
        // Make client compatible with the `tower-http` layers.
        .layer(HttpClientLayer)
        .service(reqwest::Client::new());
    // Execute request by using this service.
    let response = client
        .call(
            http::request::Builder::new()
                .method(http::Method::GET)
                .uri("http://ip.jsontest.com")
                .body(reqwest::Body::default())?,
        )
        .await?;

    let bytes = response.into_body().collect().await?.to_bytes();
    let value: Value = serde_json::from_slice(&bytes)?;
    println!("{value:#?}");

    Ok(())
}

Dependencies

~5–16MB
~203K SLoC