TrueLayer / reqwest-middleware

Wrapper around reqwest to allow for client middleware chains.
Apache License 2.0
257 stars 78 forks source link

How to Get Response Body in middleware #178

Open caibirdme opened 2 months ago

caibirdme commented 2 months ago

I want to impl a middleware that can log request and response body

#[async_trait]
impl Middleware for LoggingMiddlware {
    async fn handle(
        &self,
        req: Request,
        extensions: &mut Extensions,
        next: Next<'_>,
    ) -> ReqResult<Response> {
        req.body().clone()
            .and_then(|b| b.as_bytes())
            .and_then(|v| {
                println!("Request: {:?}", std::str::from_utf8(v).unwrap());
                Some(())
            });
        let res = next.run(req, extensions).await;
        // since response is a future
        // how can I get its response body without changing it?
        res
    }
}