I am learning Rust and have been playing around with this example to perform an HTTP GET request and then display the data:
extern crate reqwest;
use std::io::Read;
fn run() -> Result<()> {
let mut res = reqwest::get("http://httpbin.org/get")?;
let mut body = String::new();
res.read_to_string(&mut body)?;
println!("Status: {}", res.status());
println!("Headers:\n{:#?}", res.headers());
println!("Body:\n{}", body);
Ok(())
}
I cannot understand how the variable body
is actually ending up with the correct data. For headers and status, I can see the associated functions but for the body data it just uses read_to_string
for the whole data?
The res
object has a read_to_string()
method which stores the response into the String
that you pass it in
res.read_to_string(&mut body);
Edit: imported from my comment:
reqwest::Response 0.6.2 documentation states for Read
for Response
:
Read the body of the Response
which somehow seems missing from the documentation of the current version.