如何修复特性 `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
我用 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