-
Hi. I have written a piece of middleware in which I need to extract fn call(&self, mut req: ServiceRequest) -> Self::Future {
let service = self.service.clone();
let guard = self.inner.clone();
Box::pin(async move {
let extracted_json = req.extract::<web::Json<Club>>().await;
println!("{:?}", extracted_json);
// Downstream code and call service
// ......
})
} However, when trying to extract json twice in a row like this, it fails: let extracted_json = req.extract::<web::Json<Club>>().await;
println!("Extract 1 {:?}", extracted_json);
let extracted_json2 = req.extract::<web::Json<Club>>().await;
println!("Extract 2 {:?}", extracted_json2); Extract 1 works as expected, whereas Extract 2 fails with async fn create(club: web::Json<Club>) -> WebResult<HttpResponse> {
let club = club.into_inner();
// More code
// .........
} This is extremely annoying for my application because this means I cannot extract Json in the middleware without also breaking the program somewhere further on (in the handler). Why is this? How do I resolve this? Should I open an issue addressing this? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Extractors that consume the request body can only do so once, or else we'd be potentially needing to buffer huge payloads in-memory. One of the examples shows a solution to this, which is to put a body back into the request after consuming it, allowing inner middleware and handlers to consume it again. |
Beta Was this translation helpful? Give feedback.
Extractors that consume the request body can only do so once, or else we'd be potentially needing to buffer huge payloads in-memory.
One of the examples shows a solution to this, which is to put a body back into the request after consuming it, allowing inner middleware and handlers to consume it again.