Add bytes() function to blocking::Response

This commit is contained in:
Konrad Gołuchowski
2020-01-03 20:48:23 +01:00
committed by Sean McArthur
parent f267e1435d
commit 526afe9d86

View File

@@ -5,6 +5,7 @@ use std::net::SocketAddr;
use std::pin::Pin;
use std::time::Duration;
use bytes::Bytes;
use http;
use hyper::header::HeaderMap;
#[cfg(feature = "json")]
@@ -225,6 +226,25 @@ impl Response {
})
}
/// Get the full response body as `Bytes`.
///
/// # Example
///
/// ```
/// # fn run() -> Result<(), Box<dyn std::error::Error>> {
/// let bytes = reqwest::blocking::get("http://httpbin.org/ip")?.bytes()?;
///
/// println!("bytes: {:?}", bytes);
/// # Ok(())
/// # }
/// ```
pub fn bytes(self) -> crate::Result<Bytes> {
wait::timeout(self.inner.bytes(), self.timeout).map_err(|e| match e {
wait::Waited::TimedOut(e) => crate::error::decode(e),
wait::Waited::Inner(e) => e,
})
}
/// Get the response text.
///
/// This method decodes the response body with BOM sniffing