From 2fbc20167d6656850069c6496c73969c78b0a8d2 Mon Sep 17 00:00:00 2001 From: knight42 Date: Sun, 22 Oct 2017 16:39:43 +0800 Subject: [PATCH] feat: add Response::text() --- src/response.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/response.rs b/src/response.rs index a8f81e3..d84de5a 100644 --- a/src/response.rs +++ b/src/response.rs @@ -165,6 +165,26 @@ impl Response { serde_json::from_reader(self).map_err(::error::from) } + /// Get the response text. + /// + /// # Example + /// + /// ```rust + /// # extern crate reqwest; + /// # fn run() -> Result<(), Box<::std::error::Error>> { + /// let content = reqwest::get("http://httpbin.org/range/26")?.text()?; + /// # Ok(()) + /// # } + /// ``` + pub fn text(&mut self) -> io::Result { + let len = self.headers().get::<::header::ContentLength>() + .map(|ct_len| **ct_len) + .unwrap_or(0); + let mut content = String::with_capacity(len as usize); + self.read_to_string(&mut content)?; + Ok(content) + } + /// Copy the response body into a writer. /// /// This function internally uses [`std::io::copy`] and hence will continuously read data from