title: How do I read the entire body of a Tokio-based Hyper request?
body: <p>I want to write a server using the current master branch of Hyper that saves a message that is delivered by a POST request and sends this message to every incoming GET request.</p>
  
  <p>I have this, mostly copied from the Hyper examples directory:</p>
  
  <pre><code>extern crate futures;
  extern crate hyper;
  extern crate pretty_env_logger;
  
  use futures::future::FutureResult;
  
  use hyper::{Get, Post, StatusCode};
  use hyper::header::{ContentLength};
  use hyper::server::{Http, Service, Request, Response};
  use futures::Stream;
  
  struct Echo {
      data: Vec&lt;u8&gt;,
  }
  
  impl Echo {
      fn new() -&gt; Self {
          Echo {
              data: "text".into(),
          }
      }
  }
  
  impl Service for Echo {
      type Request = Request;
      type Response = Response;
      type Error = hyper::Error;
      type Future = FutureResult&lt;Response, hyper::Error&gt;;
  
      fn call(&amp;self, req: Self::Request) -&gt; Self::Future {
          let resp = match (req.method(), req.path()) {
              (&amp;Get, "/") | (&amp;Get, "/echo") =&gt; {
                  Response::new()
                      .with_header(ContentLength(self.data.len() as u64))
                      .with_body(self.data.clone())
              },
              (&amp;Post, "/") =&gt; {
                  //self.data.clear(); // argh. &amp;self is not mutable :(
                  // even if it was mutable... how to put the entire body into it?
                  //req.body().fold(...) ?
                  let mut res = Response::new();
                  if let Some(len) = req.headers().get::&lt;ContentLength&gt;() {
                      res.headers_mut().set(ContentLength(0));
                  }
                  res.with_body(req.body())
              },
              _ =&gt; {
                  Response::new()
                      .with_status(StatusCode::NotFound)
              }
          };
          futures::future::ok(resp)
      }
  
  }
  
  
  fn main() {
      pretty_env_logger::init().unwrap();
      let addr = "127.0.0.1:12346".parse().unwrap();
  
      let server = Http::new().bind(&amp;addr, || Ok(Echo::new())).unwrap();
      println!("Listening on http://{} with 1 thread.", server.local_addr().unwrap());
      server.run().unwrap();
  }
  </code></pre>
  
  <p>How do I turn the <code>req.body()</code> (which seems to be a <code>Stream</code> of <code>Chunks</code>) into a <code>Vec&lt;u8&gt;</code>? I assume I must somehow return a <code>Future</code> that consumes the <code>Stream</code> and turns it into a single <code>Vec&lt;u8&gt;</code>, maybe with <code>fold()</code>. But I have no clue how to do that.</p>