用 POST 送出一個 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
處理 POST 過來的 JSON 資料
#[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