blob: cefb1cf0a6a2b9d5a48041adc72da824030de7d6 [file] [log] [blame]
//! This example illustrates the way to send and receive statically typed JSON.
//!
//! In contrast to the arbitrary JSON example, this brings up the full power of
//! Rust compile-time type system guaranties though it requires a little bit
//! more code.
extern crate reqwest;
#[macro_use] extern crate serde_derive;
extern crate serde_json;
#[derive(Debug, Serialize, Deserialize)]
struct Post {
id: Option<i32>,
title: String,
body: String,
#[serde(rename = "userId")]
user_id: i32,
}
fn main() -> Result<(), reqwest::Error> {
let new_post = Post {
id: None,
title: "Reqwest.rs".into(),
body: "https://docs.rs/reqwest".into(),
user_id: 1
};
let new_post: Post = reqwest::Client::new()
.post("https://jsonplaceholder.typicode.com/posts")
.json(&new_post)
.send()?
.json()?;
println!("{:#?}", new_post);
// Post {
// id: Some(
// 101
// ),
// title: "Reqwest.rs",
// body: "https://docs.rs/reqwest",
// user_id: 1
// }
Ok(())
}