How to read Request Body in Tokio-Hyper

Hi, I'm trying to build some http server using hyper.

I'm trying to get body of http request as Vec<u8>, but so far I haven't been able to get it to work.

I've checked out example server implementation, but this basically just passes request body to response body.

So far I've tried below, but this gets blocked at "before fold".

fn call(&self, req: Request) -> Self::Future {
    let (method, uri, _version, headers, body) = req.deconstruct();
    match (&method, uri.path()) {
        (&Post, "/echo") => {
            let res = Response::new();
            let input: Vec<u8> = if let Some(len) = headers.get::<ContentLength>() {
                Vec::with_capacity(**len as usize)
            } else {
                Vec::new()
            };
            debug!("before fold");
            future::result(body.fold(input, |mut acc, chunk| {
                acc.extend_from_slice(chunk.as_ref());
                Ok::<_, hyper::Error>(acc)
            }).and_then(move |body_vec| {
                debug!("body_vec ready: {}", body_vec.len());
                future::ok(res.with_body(body_vec))
            }).wait())
        },
        _ => future::ok(Response::new().with_status(StatusCode::NotFound)),
    }
}

I'm still new to Futures and I'm trying to adapt to hyper's shift to Tokio based API.
It would be great if someone can point me to the right direction.

Thanks!

1 Like

I got this working, by using CpuPool as tokio.rs getting started guide does.

Here's complete example:
https://gist.github.com/ktsujister/b2eb8b5c77d1a91196ad51a081f1c95d

I got this working without use of CpuPool now:

https://gist.github.com/ktsujister/47db23f09c964ec4cc98eb338758869c

1 Like

Thank you very much. Not many people come back to their posts to update the results. I was able to fix a problem where I was stuck for a day thanks to your two replies (I used both). Keep it up, and happy new year.