Skip to content

Commit 73b7297

Browse files
authored
Merge pull request #85 from http-rs/body-string
Read body as string convenience
2 parents 1d3a364 + 938493f commit 73b7297

File tree

4 files changed

+84
-6
lines changed

4 files changed

+84
-6
lines changed

src/body.rs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,9 @@ impl Body {
114114
/// # Examples
115115
///
116116
/// ```
117-
/// use http_types::{Body, Response, StatusCode};
117+
/// use http_types::Body;
118118
/// use async_std::io::Cursor;
119119
///
120-
/// let mut req = Response::new(StatusCode::Ok);
121-
///
122120
/// let cursor = Cursor::new("Hello Nori");
123121
/// let len = 10;
124122
/// let body = Body::from_reader(cursor, Some(len));
@@ -134,11 +132,9 @@ impl Body {
134132
///
135133
/// ```
136134
/// # use std::io::prelude::*;
137-
/// use http_types::{Body, Response, StatusCode};
135+
/// use http_types::Body;
138136
/// use async_std::io::Cursor;
139137
///
140-
/// let mut req = Response::new(StatusCode::Ok);
141-
///
142138
/// let cursor = Cursor::new("Hello Nori");
143139
/// let body = Body::from_reader(cursor, None);
144140
/// let _ = body.into_reader();
@@ -147,6 +143,29 @@ impl Body {
147143
self.reader
148144
}
149145

146+
/// Read the body as a string
147+
///
148+
/// # Examples
149+
///
150+
/// ```
151+
/// # use std::io::prelude::*;
152+
/// # fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
153+
/// # async_std::task::block_on(async {
154+
/// use http_types::Body;
155+
/// use async_std::io::Cursor;
156+
///
157+
/// let cursor = Cursor::new("Hello Nori");
158+
/// let body = Body::from_reader(cursor, None);
159+
/// assert_eq!(&body.into_string().await.unwrap(), "Hello Nori");
160+
/// # Ok(()) }) }
161+
/// ```
162+
pub async fn into_string(mut self) -> io::Result<String> {
163+
use async_std::io::ReadExt;
164+
let mut result = String::with_capacity(self.len().unwrap_or(0));
165+
self.read_to_string(&mut result).await?;
166+
Ok(result)
167+
}
168+
150169
pub(crate) fn mime(&self) -> &Mime {
151170
&self.mime
152171
}

src/headers/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ impl Headers {
3939
}
4040

4141
/// Insert a header into the headers.
42+
///
43+
/// Not that this will replace all header values for a given header name.
44+
/// If you wish to add header values for a header name that already exists
45+
/// use `Headers::append`
4246
pub fn insert(
4347
&mut self,
4448
name: impl TryInto<HeaderName>,

src/request.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,34 @@ impl Request {
198198
self.replace_body(Body::empty())
199199
}
200200

201+
/// Read the body as a string.
202+
///
203+
/// This consumes the request. If you want to read the body without
204+
/// consuming the request, consider using the `take_body` method and
205+
/// then calling `Body::into_string` or using the Request's AsyncRead
206+
/// implementation to read the body.
207+
///
208+
/// # Examples
209+
///
210+
/// ```
211+
/// # use std::io::prelude::*;
212+
/// # fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
213+
/// # async_std::task::block_on(async {
214+
/// use http_types::{Body, Url, Method, Request};
215+
/// use async_std::io::Cursor;
216+
///
217+
/// let mut req = Request::new(Method::Get, Url::parse("https://example.com").unwrap());
218+
///
219+
/// let cursor = Cursor::new("Hello Nori");
220+
/// let body = Body::from_reader(cursor, None);
221+
/// req.set_body(body);
222+
/// assert_eq!(&req.body_string().await.unwrap(), "Hello Nori");
223+
/// # Ok(()) }) }
224+
/// ```
225+
pub async fn body_string(self) -> io::Result<String> {
226+
self.body.into_string().await
227+
}
228+
201229
/// Get an HTTP header.
202230
pub fn header(&self, name: &HeaderName) -> Option<&Vec<HeaderValue>> {
203231
self.headers.get(name)

src/response.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,33 @@ impl Response {
227227
self.replace_body(Body::empty())
228228
}
229229

230+
/// Read the body as a string.
231+
///
232+
/// This consumes the response. If you want to read the body without
233+
/// consuming the response, consider using the `take_body` method and
234+
/// then calling `Body::into_string` or using the Response's AsyncRead
235+
/// implementation to read the body.
236+
///
237+
/// # Examples
238+
///
239+
/// ```
240+
/// # use std::io::prelude::*;
241+
/// # fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
242+
/// # async_std::task::block_on(async {
243+
/// use http_types::{Body, Url, Method, Response, StatusCode};
244+
/// use async_std::io::Cursor;
245+
///
246+
/// let mut resp = Response::new(StatusCode::Ok);
247+
/// let cursor = Cursor::new("Hello Nori");
248+
/// let body = Body::from_reader(cursor, None);
249+
/// resp.set_body(body);
250+
/// assert_eq!(&resp.body_string().await.unwrap(), "Hello Nori");
251+
/// # Ok(()) }) }
252+
/// ```
253+
pub async fn body_string(self) -> io::Result<String> {
254+
self.body.into_string().await
255+
}
256+
230257
/// Set the response MIME.
231258
pub fn set_content_type(&mut self, mime: Mime) -> Option<Vec<HeaderValue>> {
232259
let value: HeaderValue = mime.into();

0 commit comments

Comments
 (0)