feat(body): implement HttpBody for Request and Response
When the body type of a `Request` or `Response` implements `HttpBody`, the `Request` or `Response` itself now implements `HttpBody`. This allows writing things like `hyper::body::aggregate(req)` instead of `hyper::body::aggregate(req.into_body())`. Closes #2067
This commit is contained in:
@@ -42,7 +42,7 @@ async fn fetch_url(url: hyper::Uri) -> Result<()> {
|
||||
|
||||
// Stream the body, writing each chunk to stdout as we get it
|
||||
// (instead of buffering and printing at the end).
|
||||
while let Some(next) = res.body_mut().data().await {
|
||||
while let Some(next) = res.data().await {
|
||||
let chunk = next?;
|
||||
io::stdout().write_all(&chunk).await?;
|
||||
}
|
||||
|
||||
@@ -30,7 +30,7 @@ async fn fetch_json(url: hyper::Uri) -> Result<Vec<User>> {
|
||||
let res = client.get(url).await?;
|
||||
|
||||
// asynchronously aggregate the chunks of the body
|
||||
let body = hyper::body::aggregate(res.into_body()).await?;
|
||||
let body = hyper::body::aggregate(res).await?;
|
||||
|
||||
// try to parse as json with serde_json
|
||||
let users = serde_json::from_reader(body.reader())?;
|
||||
|
||||
@@ -17,7 +17,7 @@ async fn param_example(req: Request<Body>) -> Result<Response<Body>, hyper::Erro
|
||||
(&Method::GET, "/") | (&Method::GET, "/post") => Ok(Response::new(INDEX.into())),
|
||||
(&Method::POST, "/post") => {
|
||||
// Concatenate the body...
|
||||
let b = hyper::body::to_bytes(req.into_body()).await?;
|
||||
let b = hyper::body::to_bytes(req).await?;
|
||||
// Parse the request body. form_urlencoded::parse
|
||||
// always succeeds, but in general parsing may
|
||||
// fail (for example, an invalid post of json), so
|
||||
|
||||
@@ -40,7 +40,7 @@ async fn client_request_response(client: &Client<HttpConnector>) -> Result<Respo
|
||||
|
||||
async fn api_post_response(req: Request<Body>) -> Result<Response<Body>> {
|
||||
// Aggregate the body...
|
||||
let whole_body = hyper::body::aggregate(req.into_body()).await?;
|
||||
let whole_body = hyper::body::aggregate(req).await?;
|
||||
// Decode as JSON...
|
||||
let mut data: serde_json::Value = serde_json::from_reader(whole_body.reader())?;
|
||||
// Change the JSON...
|
||||
|
||||
Reference in New Issue
Block a user