4

I have a http-client building with hyper. And I try to send json data with method post:

fn run_client() {

    let json = json!({
                        "list": [
                        {
                          "id": 1,
                          "price": 10,                                
                        },
                        {
                          "id": 2,
                          "price": 20,
                        }]
                    }
    );

    let mut core = Core::new().unwrap();
    let client = Client::new(&core.handle());

    let uri = "http://127.0.0.1:8888/add".parse().unwrap();

    let mut req = Request::new(Method::Post, uri);

    req.headers_mut().set(ContentType::json());
    req.set_body(json);

    let post = client.request(req).and_then(|res| {
        println!("POST: {}", res.status());

        res.body().concat2()
    });

    core.run(post).unwrap();
}

But get error:

the trait `std::convert::From<serde_json::Value>` is not i mplemented for `hyper::Body`

How can I fix it and send serde_json::Value data with hyper-client?

Sergey
  • 353
  • 1
  • 3
  • 12
  • 1
    I know this doesn't answer your question but there is [reqwest](https://docs.rs/reqwest) as well. It handles all of this for you. – squiguy Feb 22 '18 at 23:17

1 Answers1

1

I fixed it by changing:

req.set_body(serde_json::to_vec(&json).unwrap());

It works because there is:

impl From<Vec<u8>> for Body
Sergey
  • 353
  • 1
  • 3
  • 12