2

I'm using boost::beast in my project. Following code is a modified version of example code. I tried to reuse flat_buffer and http::response in the following code, but the result is wrong. In the second query, the response body is concatenate of two query result. How can I solve this problem?

#include <iostream>
#include <string>

#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>
#include <boost/beast/version.hpp>
#include <boost/asio/connect.hpp>
#include <boost/asio/ip/tcp.hpp>

int main(int argc, char** argv) {
  using tcp = boost::asio::ip::tcp;       // from <boost/asio/ip/tcp.hpp>
  namespace http = boost::beast::http;    // from <boost/beast/http.hpp>
  auto const host = "www.example.com";
  auto const port = "80";
  auto const target = "/";
  int version = 11;

  boost::asio::io_context ioc;
  tcp::resolver resolver{ioc};
  tcp::socket socket{ioc};

  auto const results = resolver.resolve(host, port);

  // Make the connection on the IP address we get from a lookup
  boost::asio::connect(socket, results.begin(), results.end());

  // Set up an HTTP GET request message
  http::request<http::string_body> req{http::verb::get, target, version};
  req.set(http::field::host, host);
  req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);

  // This buffer is used for reading and must be persisted
  boost::beast::flat_buffer buffer;
  // Declare a container to hold the response
  http::response<http::dynamic_body> res;

  for (int n = 0; n < 2; n++) {
    http::write(socket, req);
    http::read(socket, buffer, res);
    // Write the message to standard out
    std::cout << res << std::endl;
    std::cout << "+++++++++++++++++\n";
  }

  // Gracefully close the socket
  boost::system::error_code ec;
  socket.shutdown(tcp::socket::shutdown_both, ec);
  return EXIT_SUCCESS;
}
PeopleMoutainPeopleSea
  • 1,492
  • 1
  • 15
  • 24
  • I have another question, why do we need buffer when doing query? – PeopleMoutainPeopleSea Jan 10 '20 at 08:41
  • It seems that I can reuse flat_buffer, but for each new query I have to create a new http::response, which I hope to avoid – PeopleMoutainPeopleSea Jan 10 '20 at 08:43
  • 1
    I found one way to clear a used http::response is to assign an empty value to it. – PeopleMoutainPeopleSea Jan 10 '20 at 09:10
  • 1
    You can answer your own question with this. Note "why do we need buffer when doing query" - that's because by definition you `read` stuff into a memory location. That is a buffer area. The reason you must supply your own _choice_ of buffer is because the library doesn't want to restrict your choices like so many other libraries do. – sehe Jan 10 '20 at 11:22

1 Answers1

3

The http::read is additive. If you want the buffer empty before the call to read you will need to do it manually.

One way is like this:

buffer.consume(buffer.size());

The beast team are always happy to help on slack if you have access to it:

http://slack.cpp.al

channel #beast

Richard Hodges
  • 68,278
  • 7
  • 90
  • 142
  • 1
    It does not work. Buffer is still has 1st query + 2nd query. – Dmitry Jun 05 '21 at 22:05
  • Can you post a link to a github repo or gist that demonstrates the problem? Or post an issue here: https://github.com/boostorg/beast/issues And I'll answer it directly. – Richard Hodges Jun 06 '21 at 07:00