Crate reqwest_middleware

Source
Expand description

This crate provides ClientWithMiddleware, a wrapper around reqwest::Client with the ability to attach middleware which runs on every request.

You’ll want to instantiate ClientWithMiddleware using ClientBuilder, then you can attach your middleware using with, finalize it with build and from then on sending requests is the same as with reqwest:

use reqwest::{Client, Request, Response};
use reqwest_middleware::{ClientBuilder, Middleware, Next, Result};
use http::Extensions;

struct LoggingMiddleware;

#[async_trait::async_trait]
impl Middleware for LoggingMiddleware {
    async fn handle(
        &self,
        req: Request,
        extensions: &mut Extensions,
        next: Next<'_>,
    ) -> Result<Response> {
        println!("Request started {:?}", req);
        let res = next.run(req, extensions).await;
        println!("Result: {:?}", res);
        res
    }
}

async fn run() {
    let reqwest_client = Client::builder().build().unwrap();
    let client = ClientBuilder::new(reqwest_client)
        .with(LoggingMiddleware)
        .build();
    let resp = client.get("https://truelayer.com").send().await.unwrap();
    println!("TrueLayer page HTML: {}", resp.text().await.unwrap());
}

Re-exports§

pub use reqwest;

Structs§

ClientBuilder
A ClientBuilder is used to build a ClientWithMiddleware.
ClientWithMiddleware
ClientWithMiddleware is a wrapper around reqwest::Client which runs middleware on every request.
Extension
A middleware that inserts the value into the Extensions during the call.
Next
Next encapsulates the remaining middleware chain to run in Middleware::handle. You can forward the request down the chain with run.
RequestBuilder
This is a wrapper around reqwest::RequestBuilder exposing the same API.

Enums§

Error

Traits§

Middleware
When attached to a ClientWithMiddleware (generally using with), middleware is run whenever the client issues a request, in the order it was attached.
RequestInitialiser
When attached to a ClientWithMiddleware (generally using with_init), it is run whenever the client starts building a request, in the order it was attached.

Type Aliases§

Result