All examples about receiving a Json by POST are like this one. Json is received, deserialized and then used directly.
#[derive(Deserialize)]
struct Info {
username: String,
}
async fn index(info: web::Json<Info>) -> impl Responder {
format!("Welcome {}!", info.username)
}
But I don't want to use info.username in this function, I need to send info, as a parameter to another function, to be received as a Info struct.
Something like this:
let resultFromJsonProcess: String = process_input(info);
But it doesn't work. The main error is:
^^^^^^^ expected `Info`, found `Json<Info>`
I thought the info is already a well-formed struct, but I don't know if an unwrap() or something is needed.
Use .into_inner()
:
info.into_inner()
Or, Json
is a tuple struct with public fields:
pub struct Json<T>(pub T);
So you can access the nested structure via the anonymous tuple field:
async fn index(info: web::Json<Info>) -> impl Responder {
format!("Welcome {}!", info.0)
}
or by pattern matching:
async fn index(Json(info): web::Json<Info>) -> impl Responder {
format!("Welcome {}!", info)
}