# Hyper: reading body and passing it to a function?

**URL:** <https://users.rust-lang.org/t/hyper-reading-body-and-passing-it-to-a-function/23143>\
**Category:** help\
**Created:** [December 11, 2018, 10:10am UTC](https://users.rust-lang.org/t/hyper-reading-body-and-passing-it-to-a-function/23143 "2018-12-11T10:10:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mjais](https://avatars.discourse-cdn.com/v4/letter/m/34f0e0/32.png) [@mjais](https://users.rust-lang.org/u/mjais)\
**Post date:** [December 11, 2018, 10:10am UTC](https://users.rust-lang.org/t/hyper-reading-body-and-passing-it-to-a-function/23143/1 "2018-12-11T10:10:53Z")

</div>

Hello,  
I have the following hyper code (hyper version 0.12.17)

```rust
fn do_stuff_with_body(body_str: String) {
    ()
}

fn read_body(req: Request<Body>) -> BoxFut {
    let mut response = Response::new(Body::empty());

    match (req.method(), req.uri().path()) {

        (&Method::POST, "/read/") => {
            println!("a1");
            let body = req.into_body();
            println!("a2");
            let value = body.concat2().wait().unwrap().to_vec();

            println!("value: {:?}", &value);
            do_stuff_with_body(String::from_utf8(value).unwrap());
            //*response.body_mut() = Body::from("aa"); //wrap_stream(mapping);
        }

        _ => {
            *response.status_mut() = StatusCode::NOT_FOUND;
        }
    };

    Box::new(future::ok(response))
}

```

I want to extract the body (here a simple String, later a more complex json document using serde).

I found the example using wait() somewhere on the web but it seems to block.  
When running, it prints "a1" and "a2" but never progresses beyond the wait() call.

What is the best practice to extract the body of a HTTP request and pass it as a String or something else to a function?

Any help very much appreciated.

Regards,  
Markus

---

<div class="post-metadata">

**Author:** ![vitalyd](https://avatars.discourse-cdn.com/v4/letter/v/edb3f5/32.png) [@vitalyd](https://users.rust-lang.org/u/vitalyd)\
**Post date:** [December 11, 2018, 12:46pm UTC](https://users.rust-lang.org/t/hyper-reading-body-and-passing-it-to-a-function/23143/2 "2018-12-11T12:46:03Z")

</div>

In the vast majority of cases, you should never call `wait()` on a future - this forces the current thread to await the future's completion, but if the current thread happens to be running the event loop/reactor, you'll deadlock. Instead, the approach is to chain continuations (by using the various `Future`/`Stream` combinators) onto the futures - those continuations receive the value (and/or error, depending on the combinator) the previous future resolved to, and you can then make use of that value.

Take a look at the "Buffering the Request Body" section on hyper's [site](https://hyper.rs/guides/server/echo/) for a feel for how this might look.

---

<div class="post-metadata">

**Author:** ![mjais](https://avatars.discourse-cdn.com/v4/letter/m/34f0e0/32.png) [@mjais](https://users.rust-lang.org/u/mjais)\
**Post date:** [December 12, 2018, 6:27am UTC](https://users.rust-lang.org/t/hyper-reading-body-and-passing-it-to-a-function/23143/3 "2018-12-12T06:27:07Z")

</div>

Thanks! That helped.  
I've rewritten parts of the code and now it works with futures.

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/steffahn/32/47569_2.png) [@steffahn](https://users.rust-lang.org/u/steffahn)\
**Post date:** [January 12, 2023, 8:51am UTC](https://users.rust-lang.org/t/hyper-reading-body-and-passing-it-to-a-function/23143/4 "2023-01-12T08:51:30Z")

</div>

Closed on January 12, 2023, 8:51am UTC


