I am trying to receive a JSON data using rocket.rs (at create_user function) as shown below.
#[macro_use]
extern crate rocket;
use anyhow::Result;
use rocket::http::Status;
use rocket::serde::json::Json;
use rocket::serde::Deserialize;
#[derive(Deserialize)]
pub struct User {
pub id: String,
pub name: String,
pub age: u32,
}
#[post("/")]
pub fn create_user(changes: Json<User>) -> Status {
println!("received");
Status::Accepted
}
#[rocket::main]
async fn main() -> Result<()> {
let _rocket = rocket::build()
.mount("/", routes![create_user])
.launch()
.await?;
Ok(())
}
But the code does not compile with the following error.
According to the documentation, T
in Json<T>
needs to implement Deserialize
, which I have already done by #[derive(Deserialize)]
.
Is there anything I am missing? Thanks in advance.
Compiling workspace v0.1.0 (/workspace)
error[E0277]: the trait bound `Json<User>: FromRequest<'_>` is not satisfied
--> src/main.rs:17:30
|
17 | pub fn create_users(changes: Json<User>) -> Status {
| ^^^^ the trait `FromRequest<'_>` is not implemented for `Json<User>`
|
= help: the following other types implement trait `FromRequest<'r>`:
&'r ContentType
&'r Host<'r>
&'r Limits
&'r Route
&'r rocket::Config
&'r rocket::State<T>
&'r rocket::http::Accept
&'r rocket::http::CookieJar<'r>
and 8 others
For more information about this error, try `rustc --explain E0277`.
I also put an link to reproduction code on Codesandbox.