I am working on a http parser, and it looks like boost.beast is a nice one. However, I still have some questions:
*** Assume HTTP Request POST data already received via boost.asio socket. Stored inside a std::string buffer.
Is there any good sample on how to extract http header fields and its value (one-after-another)? I assume it will be an iterator method, but i tried several way and still won't work.
How to extract the http body?
Thank you very much.
"BOOST BEAST" is a defense puzzle game where players defeat the invading horde of zombies by clearing blocks and summoning animals.
Boost. Asio is a cross-platform C++ library for network and low-level I/O programming that provides developers with a consistent asynchronous model using a modern C++ approach. Overview. An overview of the features included in Boost. Asio, plus rationale and design information.
Starting from a simple example: https://www.boost.org/doc/libs/develop/libs/beast/example/http/client/sync/http_client_sync.cpp
// Declare a container to hold the response
http::response<http::dynamic_body> res;
// Receive the HTTP response
http::read(socket, buffer, res);
The response object already contains all the goods:
for(auto const& field : res)
std::cout << field.name() << " = " << field.value() << "\n";
std::cout << "Server: " << res[http::field::server] << "\n";
You can also just stream the entire response object:
std::cout << res << std::endl;
std::cout << "Body size is " << res.body().size() << "\n";
To actually use the "dynamic_body", use standard Asio buffer manipulation:
#include <boost/asio/buffers_iterator.hpp>
#include <boost/asio/buffers_iterator.hpp>
std::string body { boost::asio::buffers_begin(res.body().data()),
boost::asio::buffers_end(res.body().data()) };
std::cout << "Body: " << std::quoted(body) << "\n";
Alternatively, see
beast::buffers_to_string
Obviously, things become more straight-forward when using a string_body
:
std::cout << "Body: " << std::quoted(res.body()) << "\n";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With