I am attempting to use actix_web to fetch and display the contents of a web page. The HTTP request completes successfully and I can view the webpage, but I want to read the body into a String
for printing.
I tried let my_ip: String = response.body().into();
but I get an error that says
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>`
This is what I have so far:
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(())
})
});
}
Relevant dependency versions:
[dependencies]
actix-web = "0.7"
actix = "0.7"
futures = "0.1"