@@ -114,11 +114,9 @@ impl Body {
114
114
/// # Examples
115
115
///
116
116
/// ```
117
- /// use http_types::{ Body, Response, StatusCode} ;
117
+ /// use http_types::Body;
118
118
/// use async_std::io::Cursor;
119
119
///
120
- /// let mut req = Response::new(StatusCode::Ok);
121
- ///
122
120
/// let cursor = Cursor::new("Hello Nori");
123
121
/// let len = 10;
124
122
/// let body = Body::from_reader(cursor, Some(len));
@@ -134,11 +132,9 @@ impl Body {
134
132
///
135
133
/// ```
136
134
/// # use std::io::prelude::*;
137
- /// use http_types::{ Body, Response, StatusCode} ;
135
+ /// use http_types::Body;
138
136
/// use async_std::io::Cursor;
139
137
///
140
- /// let mut req = Response::new(StatusCode::Ok);
141
- ///
142
138
/// let cursor = Cursor::new("Hello Nori");
143
139
/// let body = Body::from_reader(cursor, None);
144
140
/// let _ = body.into_reader();
@@ -147,6 +143,29 @@ impl Body {
147
143
self . reader
148
144
}
149
145
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
+
150
169
pub ( crate ) fn mime ( & self ) -> & Mime {
151
170
& self . mime
152
171
}
0 commit comments