发布一些 JSON
// This will POST a body of
// `{"lang": "rust", "body": "json"}`
#[derive(Serialize)]
struct Body<'a> {
lang: &'a str,
body: &'a str,
}
let client = reqwest::Client::new();
let res = client.post("http://httpbin.org/post")
.json(&Body {
lang: "rust",
body: "json",
})
.send()?;
了解更多关于 reqwest 的信息
处理 JSON POST
#[derive(Deserialize)]
struct Task { name: String, completed: bool }
#[post("/", data = "<task>")]
fn new(task: Json<Task>) -> Flash<Redirect> {
if task.name.is_empty() {
Flash::error(Redirect::to("/"),
"Cannot be empty.")
} else {
Flash::success(Redirect::to("/"),
"Task added.")
}
}
了解更多关于 Rocket 的信息