How do I read the entire body of a Tokio-based Hyper request?

后端 未结 3 1791
粉色の甜心
粉色の甜心 2020-11-28 15:07

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 reque

3条回答
  •  误落风尘
    2020-11-28 15:49

    Most of the answers on this topic are outdated or overly complicated. The solution is pretty simple:

    /*
        WARNING for beginners!!! This use statement
        is important so we can later use .data() method!!!
    */
    use hyper::body::HttpBody;
    
    let my_vector: Vec = request.into_body().data().await.unwrap().unwrap().to_vec();
    let my_string = String::from_utf8(my_vector).unwrap();
    

    You can also use body::to_bytes as @euclio answered. Both approaches are straight-forward! Don't forget to handle unwrap properly.

提交回复
热议问题