How to print the request body ? #1905
Answered
by
jebrosen
paulzhang5511
asked this question in
Questions
-
.attach(AdHoc::on_request("PUT Rewriter", |req, _| {
Box::pin(async move {
println!(" => Incoming request: {}", req);
})
}))
.attach(AdHoc::on_response("Response Rewriter", |req, res| {
Box::pin(async move {
if req.uri().path() == "/" {
println!(" => Rewriting response body.");
let s = res.body_mut().to_string().await.unwrap();
println!("body --> {}", s);
res.set_sized_body(None, Cursor::new(s));
}
})
})) The above code can only print the response body, I don't know how to print the request body. |
Beta Was this translation helpful? Give feedback.
Answered by
jebrosen
Sep 17, 2021
Replies: 1 comment
-
The second parameter to an If you do need to read the entire data, you should use a data guard on the appropriate route instead - but see also #775 regarding some of the limitations. In particular, it's not easy out-of-the-box to both print the body data and do something else with it, such as parsing JSON input. |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
paulzhang5511
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The second parameter to an
on_request
fairing (currently_
in your posted example) is an&mut Data
, which can be used to peek at the beginning of the request data.If you do need to read the entire data, you should use a data guard on the appropriate route instead - but see also #775 regarding some of the limitations. In particular, it's not easy out-of-the-box to both print the body data and do something else with it, such as parsing JSON input.