Skip to content

Commit 1d95b79

Browse files
committed
Fix: {Request,Response}::body_* methods take &mut self
These methods were incorrectly consuming the whole request, where they should only be consuming the Body.
1 parent b815998 commit 1d95b79

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

src/request.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ impl Request {
9393
}
9494

9595
/// Get the remote address for this request.
96+
///
9697
/// This is determined in the following priority:
9798
/// 1. `Forwarded` header `for` key
9899
/// 2. The first `X-Forwarded-For` header
@@ -102,6 +103,7 @@ impl Request {
102103
}
103104

104105
/// Get the destination host for this request.
106+
///
105107
/// This is determined in the following priority:
106108
/// 1. `Forwarded` header `host` key
107109
/// 2. The first `X-Forwarded-Host` header
@@ -304,8 +306,9 @@ impl Request {
304306
/// assert_eq!(&req.body_string().await.unwrap(), "Hello Nori");
305307
/// # Ok(()) }) }
306308
/// ```
307-
pub async fn body_string(self) -> crate::Result<String> {
308-
self.body.into_string().await
309+
pub async fn body_string(&mut self) -> crate::Result<String> {
310+
let body = self.take_body();
311+
body.into_string().await
309312
}
310313

311314
/// Read the body as bytes.
@@ -329,8 +332,9 @@ impl Request {
329332
/// assert_eq!(bytes, vec![1, 2, 3]);
330333
/// # Ok(()) }) }
331334
/// ```
332-
pub async fn body_bytes(self) -> crate::Result<Vec<u8>> {
333-
self.body.into_bytes().await
335+
pub async fn body_bytes(&mut self) -> crate::Result<Vec<u8>> {
336+
let body = self.take_body();
337+
body.into_bytes().await
334338
}
335339

336340
/// Read the body as JSON.
@@ -358,8 +362,9 @@ impl Request {
358362
/// assert_eq!(&cat.name, "chashu");
359363
/// # Ok(()) }) }
360364
/// ```
361-
pub async fn body_json<T: DeserializeOwned>(self) -> crate::Result<T> {
362-
self.body.into_json().await
365+
pub async fn body_json<T: DeserializeOwned>(&mut self) -> crate::Result<T> {
366+
let body = self.take_body();
367+
body.into_json().await
363368
}
364369

365370
/// Read the body as `x-www-form-urlencoded`.
@@ -387,8 +392,9 @@ impl Request {
387392
/// assert_eq!(&cat.name, "chashu");
388393
/// # Ok(()) }) }
389394
/// ```
390-
pub async fn body_form<T: DeserializeOwned>(self) -> crate::Result<T> {
391-
self.body.into_form().await
395+
pub async fn body_form<T: DeserializeOwned>(&mut self) -> crate::Result<T> {
396+
let body = self.take_body();
397+
body.into_form().await
392398
}
393399

394400
/// Get an HTTP header.

src/response.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,9 @@ impl Response {
261261
/// assert_eq!(&res.body_string().await.unwrap(), "Hello Nori");
262262
/// # Ok(()) }) }
263263
/// ```
264-
pub async fn body_string(self) -> crate::Result<String> {
265-
self.body.into_string().await
264+
pub async fn body_string(&mut self) -> crate::Result<String> {
265+
let body = self.take_body();
266+
body.into_string().await
266267
}
267268

268269
/// Read the body as bytes.
@@ -286,8 +287,9 @@ impl Response {
286287
/// assert_eq!(bytes, vec![1, 2, 3]);
287288
/// # Ok(()) }) }
288289
/// ```
289-
pub async fn body_bytes(self) -> crate::Result<Vec<u8>> {
290-
self.body.into_bytes().await
290+
pub async fn body_bytes(&mut self) -> crate::Result<Vec<u8>> {
291+
let body = self.take_body();
292+
body.into_bytes().await
291293
}
292294

293295
/// Read the body as JSON.
@@ -315,8 +317,9 @@ impl Response {
315317
/// assert_eq!(&cat.name, "chashu");
316318
/// # Ok(()) }) }
317319
/// ```
318-
pub async fn body_json<T: DeserializeOwned>(self) -> crate::Result<T> {
319-
self.body.into_json().await
320+
pub async fn body_json<T: DeserializeOwned>(&mut self) -> crate::Result<T> {
321+
let body = self.take_body();
322+
body.into_json().await
320323
}
321324

322325
/// Read the body as `x-www-form-urlencoded`.
@@ -344,8 +347,9 @@ impl Response {
344347
/// assert_eq!(&cat.name, "chashu");
345348
/// # Ok(()) }) }
346349
/// ```
347-
pub async fn body_form<T: DeserializeOwned>(self) -> crate::Result<T> {
348-
self.body.into_form().await
350+
pub async fn body_form<T: DeserializeOwned>(&mut self) -> crate::Result<T> {
351+
let body = self.take_body();
352+
body.into_form().await
349353
}
350354

351355
/// Set the response MIME.

0 commit comments

Comments
 (0)