Rust按 POST 请求发送 JSON和处理 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()?;
#[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.")
    }
}

猜你喜欢

转载自blog.csdn.net/qq2942713658/article/details/114745071
今日推荐