Skip to content

Commit 960df9f

Browse files
committed
{Request,Response}::body_form
1 parent e2d7630 commit 960df9f

File tree

3 files changed

+63
-5
lines changed

3 files changed

+63
-5
lines changed

src/body.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ impl Body {
193193
/// let mut req = Response::new(StatusCode::Ok);
194194
///
195195
/// let input = String::from("hello Nori!");
196-
/// req.set_body(Body::from_bytes(input));
196+
/// req.set_body(Body::from_string(input));
197197
/// ```
198198
pub fn from_string(s: String) -> Self {
199199
Self {
@@ -262,7 +262,7 @@ impl Body {
262262
/// struct Cat { name: String }
263263
///
264264
/// let cat = Cat { name: String::from("chashu") };
265-
/// let body = Body::from_json(cat)?;
265+
/// let body = Body::from_json(&cat)?;
266266
///
267267
/// let cat: Cat = body.into_json().await?;
268268
/// assert_eq!(&cat.name, "chashu");
@@ -331,7 +331,7 @@ impl Body {
331331
/// struct Cat { name: String }
332332
///
333333
/// let cat = Cat { name: String::from("chashu") };
334-
/// let body = Body::from_form(cat)?;
334+
/// let body = Body::from_form(&cat)?;
335335
///
336336
/// let cat: Cat = body.into_form().await?;
337337
/// assert_eq!(&cat.name, "chashu");

src/request.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ impl Request {
271271
///
272272
/// let cat = Cat { name: String::from("chashu") };
273273
/// let mut req = Request::new(Method::Get, Url::parse("https://example.com").unwrap());
274-
/// req.set_body(Body::from_json(cat)?);
274+
/// req.set_body(Body::from_json(&cat)?);
275275
///
276276
/// let cat: Cat = req.body_json().await?;
277277
/// assert_eq!(&cat.name, "chashu");
@@ -281,6 +281,35 @@ impl Request {
281281
self.body.into_json().await
282282
}
283283

284+
/// Read the body as `x-www-form-urlencoded`.
285+
///
286+
/// This consumes the request. If you want to read the body without
287+
/// consuming the request, consider using the `take_body` method and
288+
/// then calling `Body::into_json` or using the Request's AsyncRead
289+
/// implementation to read the body.
290+
///
291+
/// # Examples
292+
///
293+
/// ```
294+
/// # fn main() -> Result<(), http_types::Error> { async_std::task::block_on(async {
295+
/// use http_types::{Body, Url, Method, Request};
296+
/// use http_types::convert::{Serialize, Deserialize};
297+
///
298+
/// #[derive(Debug, Serialize, Deserialize)]
299+
/// struct Cat { name: String }
300+
///
301+
/// let cat = Cat { name: String::from("chashu") };
302+
/// let mut req = Request::new(Method::Get, Url::parse("https://example.com").unwrap());
303+
/// req.set_body(Body::from_form(&cat)?);
304+
///
305+
/// let cat: Cat = req.body_form().await?;
306+
/// assert_eq!(&cat.name, "chashu");
307+
/// # Ok(()) }) }
308+
/// ```
309+
pub async fn body_form<T: DeserializeOwned>(self) -> crate::Result<T> {
310+
self.body.into_form().await
311+
}
312+
284313
/// Get an HTTP header.
285314
pub fn header(&self, name: &HeaderName) -> Option<&HeaderValues> {
286315
self.headers.get(name)

src/response.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ impl Response {
300300
///
301301
/// let cat = Cat { name: String::from("chashu") };
302302
/// let mut res = Response::new(StatusCode::Ok);
303-
/// res.set_body(Body::from_json(cat)?);
303+
/// res.set_body(Body::from_json(&cat)?);
304304
///
305305
/// let cat: Cat = res.body_json().await?;
306306
/// assert_eq!(&cat.name, "chashu");
@@ -310,6 +310,35 @@ impl Response {
310310
self.body.into_json().await
311311
}
312312

313+
/// Read the body as `x-www-form-urlencoded`.
314+
///
315+
/// This consumes the request. If you want to read the body without
316+
/// consuming the request, consider using the `take_body` method and
317+
/// then calling `Body::into_json` or using the Response's AsyncRead
318+
/// implementation to read the body.
319+
///
320+
/// # Examples
321+
///
322+
/// ```
323+
/// # fn main() -> Result<(), http_types::Error> { async_std::task::block_on(async {
324+
/// use http_types::{Body, Url, Method, Response, StatusCode};
325+
/// use http_types::convert::{Serialize, Deserialize};
326+
///
327+
/// #[derive(Debug, Serialize, Deserialize)]
328+
/// struct Cat { name: String }
329+
///
330+
/// let cat = Cat { name: String::from("chashu") };
331+
/// let mut res = Response::new(StatusCode::Ok);
332+
/// res.set_body(Body::from_form(&cat)?);
333+
///
334+
/// let cat: Cat = res.body_form().await?;
335+
/// assert_eq!(&cat.name, "chashu");
336+
/// # Ok(()) }) }
337+
/// ```
338+
pub async fn body_form<T: DeserializeOwned>(self) -> crate::Result<T> {
339+
self.body.into_form().await
340+
}
341+
313342
/// Set the response MIME.
314343
pub fn set_content_type(&mut self, mime: Mime) -> Option<HeaderValues> {
315344
let value: HeaderValue = mime.into();

0 commit comments

Comments
 (0)