如何post JSON rocket 0.5.0-rc.1中的数据?
How to post JSON data in rocket 0.5.0-rc.1?
我正在尝试构建一个 POST 处理程序,它使用 rocket
(版本:0.5.0-rc.1)接收 JSON 数据。
这是我写的代码:
use rocket::{post, response::content, routes, serde::{Deserialize, Serialize}};
#[derive(Debug, Deserialize, Serialize, Clone)]
#[serde(crate = "rocket::serde")]
#[serde(rename_all = "camelCase")]
pub struct MyData {
a_field: String,
}
#[post("/route", format = "json", data = "<data>")]
fn post_my_data(data: content::Json<MyData>) -> std::io::Result<()> {
Ok(())
}
#[rocket::launch]
fn rocket() -> _ {
rocket::build()
.mount("/", routes![forster_order])
}
但是我无法编译这个:
error[E0277]: the trait bound `rocket::response::content::Json<Order>: FromData<'_>` is not satisfied
--> src\main.rs:17:25
|
17 | fn post_my_data(data: content::Json<MyData>) -> std::io::Result<()> {
| ^^^^^^^ the trait `FromData<'_>` is not implemented for `rocket::response::content::Json<Order>`
|
::: C:\Users\me\.cargo\registry\src\github.com-1ecc6299db9ec823\rocket-0.5.0-rc.1\src\data\from_data.rs:194:41
|
194 | async fn from_data(req: &'r Request<'_>, data: Data<'r>) -> Outcome<'r, Self>;
| -- required by this bound in `rocket::data::FromData::from_data`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0277`.
在 example 中,我看到在结构上使用了生命周期标识符,这是反序列化的,但是 none 我使用的类型需要生命周期标识符,所以我无法分配我的结构的标识符。
如何使用没有生命周期标识符的结构反序列化?
你的问题与生命周期无关,你使用了错误的 Json
结构。您正在使用 rocket::response::content::Json
, this can be used to set the Content-Type
of a response (see for example this). You want to use rocket::serde::json::Json
:
//...
fn post_my_data(data: rocket::serde::json::Json<MyData>) -> std::io::Result<()> {
//...
请注意,您必须启用 Rocket 功能 json
才能使用此功能。
我正在尝试构建一个 POST 处理程序,它使用 rocket
(版本:0.5.0-rc.1)接收 JSON 数据。
这是我写的代码:
use rocket::{post, response::content, routes, serde::{Deserialize, Serialize}};
#[derive(Debug, Deserialize, Serialize, Clone)]
#[serde(crate = "rocket::serde")]
#[serde(rename_all = "camelCase")]
pub struct MyData {
a_field: String,
}
#[post("/route", format = "json", data = "<data>")]
fn post_my_data(data: content::Json<MyData>) -> std::io::Result<()> {
Ok(())
}
#[rocket::launch]
fn rocket() -> _ {
rocket::build()
.mount("/", routes![forster_order])
}
但是我无法编译这个:
error[E0277]: the trait bound `rocket::response::content::Json<Order>: FromData<'_>` is not satisfied
--> src\main.rs:17:25
|
17 | fn post_my_data(data: content::Json<MyData>) -> std::io::Result<()> {
| ^^^^^^^ the trait `FromData<'_>` is not implemented for `rocket::response::content::Json<Order>`
|
::: C:\Users\me\.cargo\registry\src\github.com-1ecc6299db9ec823\rocket-0.5.0-rc.1\src\data\from_data.rs:194:41
|
194 | async fn from_data(req: &'r Request<'_>, data: Data<'r>) -> Outcome<'r, Self>;
| -- required by this bound in `rocket::data::FromData::from_data`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0277`.
在 example 中,我看到在结构上使用了生命周期标识符,这是反序列化的,但是 none 我使用的类型需要生命周期标识符,所以我无法分配我的结构的标识符。
如何使用没有生命周期标识符的结构反序列化?
你的问题与生命周期无关,你使用了错误的 Json
结构。您正在使用 rocket::response::content::Json
, this can be used to set the Content-Type
of a response (see for example this). You want to use rocket::serde::json::Json
:
//...
fn post_my_data(data: rocket::serde::json::Json<MyData>) -> std::io::Result<()> {
//...
请注意,您必须启用 Rocket 功能 json
才能使用此功能。