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;

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

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





Type Aliases§