7 releases

0.3.2 May 5, 2024
0.3.1 May 2, 2024
0.3.0 Apr 30, 2024
0.2.1 Apr 21, 2024
0.1.2 Apr 19, 2024

#9 in #http-service

Download history 244/week @ 2024-04-15 74/week @ 2024-04-22 345/week @ 2024-04-29 37/week @ 2024-05-06 6/week @ 2024-05-20

391 downloads per month
Used in tower-http-client

MIT/Apache

20KB
340 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

~6–20MB
~270K SLoC