Skip to content

Commit

Permalink
Merge pull request #85 from http-rs/body-string
Browse files Browse the repository at this point in the history
Read body as string convenience
  • Loading branch information
yoshuawuyts authored Mar 24, 2020
2 parents 1d3a364 + 938493f commit 73b7297
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 6 deletions.
31 changes: 25 additions & 6 deletions src/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,9 @@ impl Body {
/// # Examples
///
/// ```
/// use http_types::{Body, Response, StatusCode};
/// use http_types::Body;
/// use async_std::io::Cursor;
///
/// let mut req = Response::new(StatusCode::Ok);
///
/// let cursor = Cursor::new("Hello Nori");
/// let len = 10;
/// let body = Body::from_reader(cursor, Some(len));
Expand All @@ -134,11 +132,9 @@ impl Body {
///
/// ```
/// # use std::io::prelude::*;
/// use http_types::{Body, Response, StatusCode};
/// use http_types::Body;
/// use async_std::io::Cursor;
///
/// let mut req = Response::new(StatusCode::Ok);
///
/// let cursor = Cursor::new("Hello Nori");
/// let body = Body::from_reader(cursor, None);
/// let _ = body.into_reader();
Expand All @@ -147,6 +143,29 @@ impl Body {
self.reader
}

/// Read the body as a string
///
/// # Examples
///
/// ```
/// # use std::io::prelude::*;
/// # fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
/// # async_std::task::block_on(async {
/// use http_types::Body;
/// use async_std::io::Cursor;
///
/// let cursor = Cursor::new("Hello Nori");
/// let body = Body::from_reader(cursor, None);
/// assert_eq!(&body.into_string().await.unwrap(), "Hello Nori");
/// # Ok(()) }) }
/// ```
pub async fn into_string(mut self) -> io::Result<String> {
use async_std::io::ReadExt;
let mut result = String::with_capacity(self.len().unwrap_or(0));
self.read_to_string(&mut result).await?;
Ok(result)
}

pub(crate) fn mime(&self) -> &Mime {
&self.mime
}
Expand Down
4 changes: 4 additions & 0 deletions src/headers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ impl Headers {
}

/// Insert a header into the headers.
///
/// Not that this will replace all header values for a given header name.
/// If you wish to add header values for a header name that already exists
/// use `Headers::append`
pub fn insert(
&mut self,
name: impl TryInto<HeaderName>,
Expand Down
28 changes: 28 additions & 0 deletions src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,34 @@ impl Request {
self.replace_body(Body::empty())
}

/// Read the body as a string.
///
/// This consumes the request. If you want to read the body without
/// consuming the request, consider using the `take_body` method and
/// then calling `Body::into_string` or using the Request's AsyncRead
/// implementation to read the body.
///
/// # Examples
///
/// ```
/// # use std::io::prelude::*;
/// # fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
/// # async_std::task::block_on(async {
/// use http_types::{Body, Url, Method, Request};
/// use async_std::io::Cursor;
///
/// let mut req = Request::new(Method::Get, Url::parse("https://example.com").unwrap());
///
/// let cursor = Cursor::new("Hello Nori");
/// let body = Body::from_reader(cursor, None);
/// req.set_body(body);
/// assert_eq!(&req.body_string().await.unwrap(), "Hello Nori");
/// # Ok(()) }) }
/// ```
pub async fn body_string(self) -> io::Result<String> {
self.body.into_string().await
}

/// Get an HTTP header.
pub fn header(&self, name: &HeaderName) -> Option<&Vec<HeaderValue>> {
self.headers.get(name)
Expand Down
27 changes: 27 additions & 0 deletions src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,33 @@ impl Response {
self.replace_body(Body::empty())
}

/// Read the body as a string.
///
/// This consumes the response. If you want to read the body without
/// consuming the response, consider using the `take_body` method and
/// then calling `Body::into_string` or using the Response's AsyncRead
/// implementation to read the body.
///
/// # Examples
///
/// ```
/// # use std::io::prelude::*;
/// # fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
/// # async_std::task::block_on(async {
/// use http_types::{Body, Url, Method, Response, StatusCode};
/// use async_std::io::Cursor;
///
/// let mut resp = Response::new(StatusCode::Ok);
/// let cursor = Cursor::new("Hello Nori");
/// let body = Body::from_reader(cursor, None);
/// resp.set_body(body);
/// assert_eq!(&resp.body_string().await.unwrap(), "Hello Nori");
/// # Ok(()) }) }
/// ```
pub async fn body_string(self) -> io::Result<String> {
self.body.into_string().await
}

/// Set the response MIME.
pub fn set_content_type(&mut self, mime: Mime) -> Option<Vec<HeaderValue>> {
let value: HeaderValue = mime.into();
Expand Down

0 comments on commit 73b7297

Please sign in to comment.