From 6b75cbc7f251fc84c99a5afb98cd2485bcdea1c9 Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Mon, 23 Dec 2019 11:01:37 -0800 Subject: [PATCH] Add `Response::bytes_stream()` This converts the `Response` into a `Stream` of `Bytes`. Closes #734 --- src/async_impl/response.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/async_impl/response.rs b/src/async_impl/response.rs index ec9f430ef..17d05c020 100644 --- a/src/async_impl/response.rs +++ b/src/async_impl/response.rs @@ -292,6 +292,33 @@ impl Response { } } + /// Convert the response into a `Stream` of `Bytes` from the body. + /// + /// # Example + /// + /// ``` + /// use futures_util::StreamExt; + /// + /// # async fn run() -> Result<(), Box> { + /// let mut stream = reqwest::get("http://httpbin.org/ip") + /// .await? + /// .bytes_stream(); + /// + /// while let Some(item) = stream.next().await { + /// println!("Chunk: {:?}", item?); + /// } + /// # Ok(()) + /// # } + /// ``` + /// + /// # Optional + /// + /// This requires the optional `stream` feature to be enabled. + #[cfg(feature = "stream")] + pub fn bytes_stream(self) -> impl futures_core::Stream> { + self.body + } + // util methods /// Turn a response into an error if the server returned an error.