Beast: response<string_body> has empty body

Created on 5 Dec 2018  路  2Comments  路  Source: boostorg/beast

I'm attempting to parse a string http response (returned from libcurl) using a response_parser<string_body> object, but the parser.get().body() seems to always be empty.

I have a short code snippet the reproduces my issue.

Expected: parser.get().body() = "I'm a little teapot"
Observed: parser.get().body() = ""

string s =
    "HTTP/1.1 200 OK\r\n"
    "Content-Type: text/plain; charset=us-ascii\r\n"
    "Server: Fake Server\r\n"
    "Content-Length: 19\r\n"
    "\r\n"
    "I'm a little teapot\r\n"
    "\r\n";

boost::beast::error_code ec;
boost::beast::http::response_parser<string_body> p;

p.put(boost::asio::buffer(s), ec);

boost::beast::http::response<string_body> r = p.get();

// Prints: Ec=system:0
cout << "Ec=" << ec << endl;

// I'm able to get response headers
// Prints: Content-Type=text/plain; charset=us-ascii
cout << "Content-Type=" << r[field::content_type] << endl;

// But unable to get the response body
// Prints: body=
cout << "Body=" << r.body() << endl;

Any insight or tips would be greatly appreciated. I've spent more time than I'd care to admit wrestling with this.

Most helpful comment

Given the code above, these asserts should not trigger:

assert(p.is_header_done());
assert(! p.is_done()); // means you aren't done reading the message
assert(! p.eager()); // default is non-eager parsing

and adding this code before the call to put

p.eager(true);

should resolve your issue.

See:
https://www.boost.org/doc/libs/1_68_0/libs/beast/doc/html/beast/ref/boost__beast__http__basic_parser/eager/overload2.html

This is also explained here:
https://www.boost.org/doc/libs/1_68_0/libs/beast/doc/html/beast/using_http/buffer_oriented_parsing.html

All 2 comments

Given the code above, these asserts should not trigger:

assert(p.is_header_done());
assert(! p.is_done()); // means you aren't done reading the message
assert(! p.eager()); // default is non-eager parsing

and adding this code before the call to put

p.eager(true);

should resolve your issue.

See:
https://www.boost.org/doc/libs/1_68_0/libs/beast/doc/html/beast/ref/boost__beast__http__basic_parser/eager/overload2.html

This is also explained here:
https://www.boost.org/doc/libs/1_68_0/libs/beast/doc/html/beast/using_http/buffer_oriented_parsing.html

doh! Thanks @vinniefalco - works like a charm. This is user error, so closing issue.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ksergey picture ksergey  路  6Comments

djarek picture djarek  路  6Comments

monada99 picture monada99  路  5Comments

xemwebe picture xemwebe  路  6Comments

nguoithichkhampha picture nguoithichkhampha  路  7Comments