簡體   English   中英

如何修復特征`std :: convert :: From <serde_json::Value> 沒有為hyper :: Body實施?

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

我有一個帶有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`

我該如何解決它並通過超級客戶端發送serde_json::Value數據?

我通過更改來修復它:

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

之所以有效,是因為有:

impl From<Vec<u8>> for Body

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM