简体   繁体   中英

How to fix the trait `std::convert::From<serde_json::Value>` is not implemented for `hyper::Body`?

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM