How to get http request body from boost beast http_parser?

2k Views Asked by At

How to get request body from a http_request? I tried http_request.body(), it's not work, http_request.body() returns an empty string.

#include "pch.h"
#include <iostream>
#include <boost/beast.hpp>

int main()
{

    std::string s =
        "POST /cgi/message.php HTTP/1.1\r\n"
        "Content-Length: 5\r\n"
        "\r\n"
        "abcde";
    boost::system::error_code ec;
    boost::beast::http::request_parser<boost::beast::http::string_body> p;
    p.put(boost::asio::buffer(s), ec);

    boost::beast::http::request<boost::beast::http::string_body> r = p.get();
    auto b = r.body(); // !!! b is an empty string! can't get request body! 


    std::cout << "Hello World!\n"; 
}

EDIT: Now, I know why can't I get request body, as default, the first time call http_parser.put(), http_parser only parse http line and http fields, next time call http_parser.put(), http_parser will parse http message body. if we have a full http message already contains http body, before put full http request to http_parser, we must call http_request.eager(true) to control http_parser parse the http body now.

0

There are 0 best solutions below