POST de un 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()?;
Saber más sobre reqwest
Recibir un POST con 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.")
}
}
Saber más sobre Rocket