I want to use the Axum framework of Rust in the following way: Suppose there is a request incoming with a request body. I want to obtain the request body, inspect it, possibly change parts of it and use that to create a response.
However, I'm stuck at obtaining the request body in a appropriate way to be able to handle it. I guess it should be enough if I know how to convert to something like a vector of bytes or being able to print its content. Please note it is ok to assume that the body would consist of something which can be converted to a UTF-8 string or JSON.
use axum::body::Body;
use axum::http::Request;
use axum::response::IntoResponse;
use axum::Json;
use hyper::StatusCode;
use serde_json::json;
pub async fn modder(mut request: Request<Body>) -> impl IntoResponse {
let body = request;
// What to I need to do here, to obtain the body in an appropiate way (e.g. bytes)?
let response = json!({
"data": {
"values": "transformed body",
},
});
(StatusCode::OK, Json(response))
}
How do I need to change the code? I would appreciate answer which are also in line with the newest version of axum.
Looks like this link
https://docs.rs/axum/latest/axum/extract/index.html
Has examples of what you might be looking for. Especially the part here
https://docs.rs/axum/latest/axum/extract/index.html#the-order-of-extractors
Might need to implement this trait FromRequest for your desired data structure.
https://docs.rs/axum/latest/axum/extract/trait.FromRequest.html
Which involves this Request from a different crate, http:
https://docs.rs/http/1.1.0/http/request/struct.Request.html
Then Axum library uses that to wrap their Body type here https://docs.rs/axum/latest/axum/body/struct.Body.html
Probably many ways to do this, the idiomatic Rust way would be to implement From, the idiomatic Axum approach might be to implement FromRequest for your data structure.
you might need to define input and output data structures to handle FromRequest and IntoResponse respectively.
Might need a different modder
(I suggest verb names for functions btw, use nouns like modder for data) for each kind of modification (presumably, route)