You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I want to impl a middleware that can log request and response body
#[async_trait]implMiddlewareforLoggingMiddlware{asyncfnhandle(&self,req:Request,extensions:&mutExtensions,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
}}
The text was updated successfully, but these errors were encountered:
Any updates on this one? I had the same scenario, trying to log the response body within the logger middleware but the current design just doesn't support this. In the end, I needed to pull out my logging logic out of the middleware and wrap around HTTP client's execute method in order to invoke the .bytes() method.
I guess the culprit is that body streaming methods such as .bytes() are consuming the whole Response object (self) so it becomes unusable after it is invoked.
I am interested in adding a body trace to my OTLP span but that seems even more difficult as fn on_request_end ofReqwestOtelSpanBackend is not even async.
I want to impl a middleware that can log request and response body
The text was updated successfully, but these errors were encountered: