Hyper response body mapping shows body size

I tried to display the contents (body) of the url as text using Hyper

extern crate hyper; use hyper::client::Client; use std::io::Read; fn main () { let client = Client::new(); let mut s = String::new(); let res = client.get("https://www.reddit.com/r/programming/.rss") .send() .unwrap() .read_to_string(&mut s) .unwrap(); println!("Result: {}", res); } 

But running this script just returns the size of the body:

 Result: 22871 

What I did wrong? I didn’t understand something?

+6
source share
2 answers

You read the result of get in s , but you print the result of this function, which is the number of bytes read. See the documentation for Read::read_to_string .

So the code that prints the extracted content is:

 extern crate hyper; use hyper::client::Client; use std::io::Read; fn main () { let client = Client::new(); let mut s = String::new(); let res = client.get("https://www.reddit.com/r/programming/.rss") .send() .unwrap() .read_to_string(&mut s) .unwrap(); println!("Result: {}", s); } 
+11
source

Starting with hyper 0.12, the following works on the condition that the web page is valid UTF-8:

 extern crate hyper; extern crate hyper_tls; use hyper::Client; use hyper::rt::{self, Future, Stream}; use hyper_tls::HttpsConnector; fn main() { rt::run(rt::lazy(|| { let https = HttpsConnector::new(4).unwrap(); let client = Client::builder().build::<_, hyper::Body>(https); client.get("https://www.reddit.com/r/programming/.rss".parse().unwrap()) .and_then(|res| { println!("status {}", res.status()); res.into_body().concat2() }).map(|body| { println!("Body {}", String::from_utf8(body.to_vec()).unwrap()); }) .map_err(|err| { println!("error {}", err) }) })); } 
0
source

All Articles