我正在尝试使用actix_web来获取和显示网页的内容。HTTP 请求成功完成,我可以查看网页,但我想将正文读入String
用于打印。
我试过let my_ip: String = response.body().into();
了,但我得到一个错误,上面写着
error[E0277]: the trait bound `std::string::String: std::convert::From<actix_web::httpmessage::MessageBody<actix_web::client::response::ClientResponse>>` is not satisfied
--> src/main.rs:16:53
|
16 | let my_ip: String = response.body().into();
| ^^^^ the trait `std::convert::From<actix_web::httpmessage::MessageBody<actix_web::client::response::ClientResponse>>` is not implemented for `std::string::String`
|
= help: the following implementations were found:
<std::string::String as std::convert::From<&'a str>>
<std::string::String as std::convert::From<std::borrow::Cow<'a, str>>>
<std::string::String as std::convert::From<std::boxed::Box<str>>>
<std::string::String as std::convert::From<trust_dns_proto::error::ProtoError>>
= note: required because of the requirements on the impl of `std::convert::Into<std::string::String>` for `actix_web::httpmessage::MessageBody<actix_web::client::response::ClientResponse>`
这是我到目前为止所拥有的:
use actix;
use actix_web::{client, HttpMessage};
use futures::future::Future;
fn main() {
actix::run(|| {
client::get("http://ipv4.canhasip.com/")
.header("User-Agent", "Actix-web")
.finish()
.unwrap()
.send()
.map_err(|_| ())
.and_then(|response| {
println!("Response: {:?}", response);
// error occurs here
let my_ip: String = response.body().into();
Ok(())
})
});
}
相关依赖版本:
[dependencies]
actix-web = "0.7"
actix = "0.7"
futures = "0.1"