title: C++ Boost Beast empty response body
body: <p>I'm playing around with the Beast part of Boost and I'm trying to create a simple http client that can be used to <em>/GET</em> the content form a web resource (simple HTML or REST services).</p>
  
  <p>I've used the example that can be found in <a href="https://github.com/boostorg/beast/blob/develop/example/http/client/sync/http_client_sync.cpp" rel="nofollow noreferrer">github repo</a> of beast for a sync http client and moved it to a simple class called <strong>http_client</strong>:<br>
  Header:</p>
  
  <pre><code>#ifndef HTTP_CLIENT_HPP
  #define HTTP_CLIENT_HPP
  
  #include &lt;string&gt;
  #include &lt;boost/beast/core.hpp&gt;
  #include &lt;boost/beast/http.hpp&gt;
  
  namespace playground
  {
      namespace net
      {
  
          namespace beast = boost::beast;
  
          class http_client
          {
          public:
              beast::http::response&lt;beast::http::string_body&gt;
              get(std::string base_url,
                  std::string path,
                  std::string port = "80",
                  unsigned short http_version = 11);
          };
  
      }
  }
  
  #endif
  </code></pre>
  
  <p>Implementation:  </p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;cstdlib&gt;
  #include &lt;stdexcept&gt;
  #include &lt;boost/beast/core.hpp&gt;
  #include &lt;boost/beast/http.hpp&gt;
  #include &lt;boost/asio/connect.hpp&gt;
  #include &lt;boost/asio/ip/tcp.hpp&gt;
  
  #include "http_client.hpp"
  
  namespace playground
  {
      namespace net
      {
  
          namespace beast = boost::beast;
          namespace asio = boost::asio;
  
          beast::http::response&lt;beast::http::string_body&gt; 
          http_client::get(std::string base_url,
                           std::string path,
                           std::string port,
                           unsigned short http_version)
          {
              std::cout &lt;&lt; "HttpClient::getAsync()" &lt;&lt; std::endl;
              beast::http::response &lt;boost::beast::http::string_body&gt; httpResponse;
              asio::io_context ioContext;
              asio::ip::tcp::resolver tcpResolver
              { ioContext };
              asio::ip::tcp::socket tcpSocket
              { ioContext };
              beast::http::request &lt;beast::http::string_body&gt; httpRequest(beast::http::verb::get, 
                                                                          path, 
                                                                          http_version);
              beast::error_code connectionError;
              beast::error_code readError;
              beast::flat_buffer responseBuffer;
  
              try
              {
  
                  auto const ipAddresses = tcpResolver.resolve(base_url, port);
  
                  asio::connect(tcpSocket,
                                ipAddresses.begin(),
                                ipAddresses.end());
  
                  if (tcpSocket.is_open())
                  {
                      httpRequest.set(beast::http::field::host, base_url);
                      httpRequest.set(beast::http::field::user_agent,
                                      "http_client");
                      httpRequest.set(beast::http::field::accept, "text/xml");
  
                      beast::http::write(tcpSocket, httpRequest);
  
                      beast::http::read(tcpSocket,
                                        responseBuffer,
                                        httpResponse,
                                        readError);
  
                      if (readError != beast::http::error::end_of_stream)
                      {
                          throw std::runtime_error(readError.message());
                      }
  
                      tcpSocket.shutdown(asio::ip::tcp::socket::shutdown_both,
                                         connectionError);
  
                      if (connectionError
                              &amp;&amp; connectionError != beast::errc::not_connected)
                      {
                          throw beast::system_error(connectionError);
                      }
                  }
                  else
                  {
                      throw std::runtime_error("Unable to open connection!");
                  }
  
              }
              catch (std::exception const&amp; ex)
              {
                  throw;
              }
              return httpResponse;
          }
  
      }
  }
  </code></pre>
  
  <p>The problem now is that the given string_body is empty although the http status code is 200.</p>
  
  <p>Example call:</p>
  
  <pre><code>playground::net::http_client client;
  std::string url("www.google.de");
  auto response = client.get(url, "/", "443");
  
  std::cout &lt;&lt; "Response: " &lt;&lt; response &lt;&lt; std::endl;
  std::cout &lt;&lt; "Response body: " &lt;&lt; response.body() &lt;&lt; std::endl;
  </code></pre>
  
  <p>Is there something missing in the call?</p>