我正在尝试使用 Postman 向我的 Rust API 发出 POST 请求:
#![feature(proc_macro_hygiene, decl_macro)]
#[macro_use]
extern crate rocket;
#[macro_use]
extern crate serde_derive;
use rocket_contrib::json::{Json, JsonValue};
#[derive(Serialize, Deserialize)]
struct Response {
status: String,
}
#[get("/")]
fn index() -> &'static str {
"Hello, world!"
}
#[post("/register", format = "json", data = "<register>")]
fn user_create(register: Json<Response>) -> Json<Response> {
Json(Response {
status: "works".to_string(),
})
}
fn main() {
rocket::ignite()
.mount("/", routes![index, user_create])
.launch();
}
货物.toml:
[package]
name = "hello-rocket"
version = "0.1.0"
authors = [""]
edition = "2018"
[dependencies]
rocket = "0.4.6"
serde = { version = "1.0", feature = ["derive"]}
serde_json = "1.0"
serde_derive = "1.0"
[dependencies.rocket_contrib]
version = "0.4.6"
default-features = false
features = ["json"]
我Content-Type: application/json
在 Postman 中配置了一个标头,我的请求正文是{"status": "running"}
:
我正进入(状态
Error: Couldn't parse JSON body: Error("EOF while parsing a value", line: 1, column: 0)