seanmonstar / reqwest

An easy and powerful Rust HTTP Client
https://docs.rs/reqwest
Apache License 2.0
9.98k stars 1.13k forks source link

How do I post nested JSON? #928

Open prographo opened 4 years ago

prographo commented 4 years ago

What about nested JSON, i.e. params fields?

// This will POST a body of `{"lang":"rust","body":"json"}`
let mut map = HashMap::new();
map.insert("lang", "rust");
map.insert("body", "json");

let client = reqwest::Client::new();
let res = client.post("http://httpbin.org/post")
    .json(&map)
    .send()
    .await?;

I want to do the equivalent of this:

curl --connect-timeout 60 -m 120 -s -X POST -H "Content-Type: application/json" -d '{"id":1,"jsonrpc":"2.0","method":"callMax","params":[{"commitment":"never"}]}' http://api.zeebee.com/

Is there a way to just pass a json object https://github.com/serde-rs/json

x1957 commented 4 years ago
use serde::Serialize;
#[derive(Serialize)]
struct BodyWithJson<'a, T: Serialize> {
    pub lang: &'a str,
    pub body: &'a T,
}