-
In this code I would like to update the response body, the res variable towards bottom of code, which is of type B. After a check for its content type I would like to take the json response deserialize it into the rust type and mutate the data. Similar action is successful with the req but cannot figure out how to do it with the res. Is there no way?
|
Beta Was this translation helpful? Give feedback.
Replies: 7 comments 2 replies
-
I've managed to get a lot closer now, but my return type is now screwed up and I get the error shown because HttpResponse::build is returning a HttpResponse, but my return type is B.
|
Beta Was this translation helpful? Give feedback.
-
Try this: Box::pin(async move {
let res = fut.call(req).await?;
if just_pass {
return Ok(res.map_into_left_body());
}
...
body_obj.msg = format!("{}. I modified the response.", body_obj.msg);
let response = HttpResponse::build(status)
.content_type("application/json")
.body(body_obj)
.finish() // maybe it's extra
// constructed responses map to "right" body
.map_into_right_body();
let (request, _pl) = req.into_parts();
Ok(ServiceResponse::new(request, response))
})
Also too many |
Beta Was this translation helpful? Give feedback.
-
There's no finish function showing up, but my call function's sig is not returning an EitherBody and not sure Transform would accept it. |
Beta Was this translation helpful? Give feedback.
-
The main thing I don't get is that BoxBody implements MessageBody and the B is constrained to need an impl of MessageBody. So then why isn't a return of BoxBody sufficient to be B? |
Beta Was this translation helpful? Give feedback.
-
Actually |
Beta Was this translation helpful? Give feedback.
-
I've almost got it to work by using EitherBody, but now it gets stuck on an infinite loop on the poll_next function of the impl MessageBody for ResponseMessage. But I need this impl so I can do try_into_bytes. Any ideas?
|
Beta Was this translation helpful? Give feedback.
-
Yeah I had to switch to using the json function instead of body for HttpResponse building, but yes removing the impl MessageBody works. Thank you. |
Beta Was this translation helpful? Give feedback.
Firstly, using
EitherBody
is the right solution here, following a similar pattern to this case from the examples repo.Check out the docs for
poll_next
to see what should be returned. In this case, you have an infinite loop because you never returnOk(None)
.Though, I'd advise against using a custom impl here, since
String
implementsMessageBody
already.