reqwest-middleware icon indicating copy to clipboard operation
reqwest-middleware copied to clipboard

How to Get Response Body in middleware

Open caibirdme opened this issue 7 months ago • 2 comments

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
    }
}

caibirdme avatar Jul 31 '24 03:07 caibirdme