如何修复“std :: convert :: From <serde_json :: Value>”特征未实现于 “hyper :: Body”?

4

我正在使用Hyper构建一个HTTP客户端。我尝试使用POST方法发送JSON数据:

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();
}

但是会出现错误:
the trait `std::convert::From<serde_json::Value>` is not i mplemented for `hyper::Body`

我该如何修复并使用hyper-client发送serde_json::Value数据?

1
我知道这并不是你问题的答案,但是也可以使用reqwest。它可以为你处理所有这些事情。 - squiguy
1个回答

1
我通过更改以下内容来修复它:


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

它能够工作是因为有以下内容:
impl From<Vec<u8>> for Body

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接