json_dynamic.rs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. //! This example illustrates the way to send and receive arbitrary JSON.
  2. //!
  3. //! This is useful for some ad-hoc experiments and situations when you don't
  4. //! really care about the structure of the JSON and just need to display it or
  5. //! process it at runtime.
  6. // This is using the `tokio` runtime. You'll need the following dependency:
  7. //
  8. // `tokio = { version = "1", features = ["full"] }`
  9. #[tokio::main]
  10. async fn main() -> Result<(), reqwest::Error> {
  11. let echo_json: serde_json::Value = reqwest::Client::new()
  12. .post("https://jsonplaceholder.typicode.com/posts")
  13. .json(&serde_json::json!({
  14. "title": "Reqwest.rs",
  15. "body": "https://docs.rs/reqwest",
  16. "userId": 1
  17. }))
  18. .send()
  19. .await?
  20. .json()
  21. .await?;
  22. println!("{:#?}", echo_json);
  23. // Object(
  24. // {
  25. // "body": String(
  26. // "https://docs.rs/reqwest"
  27. // ),
  28. // "id": Number(
  29. // 101
  30. // ),
  31. // "title": String(
  32. // "Reqwest.rs"
  33. // ),
  34. // "userId": Number(
  35. // 1
  36. // )
  37. // }
  38. // )
  39. Ok(())
  40. }