Added JSON examples

This commit is contained in:
Vlad Frolov
2018-05-02 17:34:15 +03:00
committed by Sean McArthur
parent 3a5af68b92
commit 276c3d6594
2 changed files with 82 additions and 0 deletions

40
examples/json_dynamic.rs Normal file
View File

@@ -0,0 +1,40 @@
//! This example illustrates the way to send and receive arbitrary JSON.
//!
//! This is useful for some ad-hoc experiments and situations when you don't
//! really care about the structure of the JSON and just need to display it or
//! process it at runtime.
extern crate reqwest;
#[macro_use] extern crate serde_json;
fn main() -> Result<(), reqwest::Error> {
let echo_json: serde_json::Value = reqwest::Client::new()
.post("https://jsonplaceholder.typicode.com/posts")
.json(
&json!({
"title": "Reqwest.rs",
"body": "https://docs.rs/reqwest",
"userId": 1
})
)
.send()?
.json()?;
println!("{:#?}", echo_json);
// Object(
// {
// "body": String(
// "https://docs.rs/reqwest"
// ),
// "id": Number(
// 101
// ),
// "title": String(
// "Reqwest.rs"
// ),
// "userId": Number(
// 1
// )
// }
// )
Ok(())
}