<p>I recently upgraded from Boost 1.67.0 to Boost 1.75.0 and immediately ran into issues with boost beast in code that talks to a REST API.</p>
<p>The code was previously working, but now it appears to be sending garbage to the server for the content and I have absolutely no clue why.</p>
<p>Here is the code, which posts a JSON string to the Kubernetes API to specify a custom resource. The specifics of the REST API are immaterial as the Kubernetes API server can't even read the boost POST request:</p>
<pre><code>#include &lt;string&gt;
#include &lt;iostream&gt;
#include &lt;sstream&gt;
#include &lt;fstream&gt; 
#include &lt;boost/beast/core.hpp&gt;
#include &lt;boost/beast/version.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 &lt;boost/asio/ssl/stream.hpp&gt; 
#include &lt;boost/asio/ssl/error.hpp&gt;
#include &lt;boost/property_tree/ptree.hpp&gt;
#include &lt;boost/property_tree/json_parser.hpp&gt;


namespace bip = boost::asio::ip;
namespace bhttp = boost::beast::http;
namespace ssl = boost::asio::ssl;

void postServiceEndpoint(std::string topicName, std::string url, 
                         std::string host, std::string port, std::string discoveryNamespace)
{
  boost::asio::io_context context;
  boost::asio::ip::tcp::resolver resolver(context);  
  ssl::context sslCtx({ssl::context::sslv23_client});
  boost::asio::ssl::stream&lt;boost::beast::tcp_stream&gt; sslStream(context, sslCtx);
  
 
  auto const results = resolver.resolve(host, port);
  SSL_set_tlsext_host_name(sslStream.native_handle(), host.c_str());
  boost::beast::get_lowest_layer(sslStream).connect(results);
  sslStream.handshake(ssl::stream_base::client);


  //Load the bearer token for authenticating with K8s... 
  std::ifstream t(&quot;/var/run/secrets/kubernetes.io/serviceaccount/token&quot;);
  std::string str((std::istreambuf_iterator&lt;char&gt;(t)), 
                  std::istreambuf_iterator&lt;char&gt;());
  std::string bearerToken = str; 

  std::string target = &quot;/apis/sdsendpoints.net/v1/namespaces/&quot; + discoveryNamespace + &quot;sdsendpoints&quot;; 
  //Because the endpoint hasn't been created yet, we cant use it in the target
  //string, but if we want to retrieve the endpoint later, we have to use its name
  //in the target string... Kubernetes's REST API be weird like that. 
  bhttp::request&lt;bhttp::string_body&gt; request(bhttp::verb::post, target, HTTPV1DOT1);
  request.set(bhttp::field::host, host);
  request.set(&quot;Content-Type&quot;, &quot;application/json&quot;);
  request.set(&quot;Authorization&quot;, &quot;Bearer &quot; + bearerToken);
   
  boost::property_tree::ptree requestTree;
  requestTree.put(&quot;apiVersion&quot;, &quot;sdsendpoints.net/v1&quot;);
  requestTree.put(&quot;kind&quot;, &quot;SdsEndpoint&quot;);
  requestTree.put(&quot;metadata.name&quot;, topicName);
  requestTree.put(&quot;spec.endpointURL&quot;, url);
      
  std::stringstream jsonStream;
  boost::property_tree::write_json(jsonStream, requestTree);
  request.body() = jsonStream.str();
  request.prepare_payload();
  std::cout &lt;&lt; &quot;REQUEST: \n&quot; &lt;&lt; request &lt;&lt; std::endl;
  bhttp::write(sslStream, request);
  boost::beast::flat_buffer buffer;
  bhttp::response&lt;bhttp::string_body&gt; response;
  bhttp::read(sslStream, buffer, response);         
  if(response.result_int() &gt;= 400)
  {
       std::cout &lt;&lt; &quot;Got failure on post endpoint: &quot; &lt;&lt; response.result_int() &lt;&lt; &quot;: &quot; &lt;&lt; response.result() &lt;&lt; &quot; : &quot; &lt;&lt; response.body() &lt;&lt; std::endl;
  }
  //Cleanup the SSL socket...
  boost::system::error_code ec;
  sslStream.shutdown(ec);
  if(ec == boost::asio::error::eof)
  {
    //This is fine. I am okay with the events that are unfolding currently.
    ec.assign(0, ec.category());
  }
  if(ec)
  {
    std::cout &lt;&lt; &quot;Got error code: &quot; &lt;&lt; ec &lt;&lt; &quot; on socket cleanup in SSL shutdown&quot; &lt;&lt; std::endl;
  }
  sslStream.lowest_layer().shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
  if(ec)
  { 
    std::cout &lt;&lt; &quot;Got error code: &quot; &lt;&lt; ec &lt;&lt; &quot; on socket cleanup in TCP socket shutdown.&quot; &lt;&lt; std::endl;
  }

}
</code></pre>
<p>Running this code gives the following output for the boost HTTP request:</p>
<pre><code>POST apis/sdsendpoints.net/v1/namespaces/sds-test/sdsendpoints HTTP/1.1
Host: kubernetes
Authorization: Bearer &lt;REDACTED&gt; 

Content-Type: application/json
Content-length: 133

{&quot;apiVersion&quot; : &quot;sdsendpoints.net/v1&quot;, &quot;kind&quot;:&quot;SdsEndpoint&quot;,&quot;metadata&quot;:{&quot;name&quot;:&quot;sds-tester&quot;},&quot;spec&quot;:{&quot;endpointURL&quot;:&quot;tcp://test-host:31337&quot;}}
</code></pre>
<p>Which appears to be a completely valid HTTP request.</p>
<p>However, what I get back from the server now (under 1.75.0 that I <em>did not</em> get under 1.67.0) is:</p>
<pre><code>Got failure on post endpoint: 400: Bad Request : {&quot;kind&quot;:&quot;Status&quot;,&quot;apiVersion&quot;:&quot;v1&quot;,&quot;metadata&quot;:{},&quot;status&quot;:&quot;Failure&quot;,&quot;message&quot;:&quot;the object provided is unrecognized (must be of type SdsEndpoint): couldn't get version/kind; json parse error: unexpected end of JSON input (\u003cempty\u003e)&quot;,&quot;reason&quot;:&quot;BadRequest&quot;,&quot;code&quot;:400}
</code></pre>
<p>Which seems to indicate the actual HTTP request is getting scrambled somehow.</p>
<p>Additionally, while cleaning up the SSL socket I get the error code asio.ssl:2, which makes me wonder if the scrambling is due to some kind of error in setting up the connection. However, the code appears to follow the boost 1.75.0 example for synchronous HTTP SSL connections, and both this version and the 1.67.0 form that uses a TCP socket in the ssl stream instead of a boost tcp stream fail in the same way (with the 400 error).</p>
<p>Thinking it might be an issue in boost property tree, I rewrote that section using boost JSON and still got the same 400 error, suggesting that the problem is not in the JSON string itself but whatever boost beast is doing in this newer version with the request.</p>
<p>As a final sanity check I manually http POST'd using curl:</p>
<pre><code>curl -x POST -h &quot;Host: kubernetes&quot; -H &quot;Authorization: Bearer &lt;REDACTED&gt;&quot; -H &quot;Content-Type: application/json&quot; --data '{&quot;apiVersion&quot; : &quot;sdsendpoints.net/v1&quot;, &quot;kind&quot;:&quot;SdsEndpoint&quot;,&quot;metadata&quot;:{&quot;name&quot;:&quot;sds-tester&quot;},&quot;spec&quot;:{&quot;endpointURL&quot;:&quot;tcp://test-host:31337&quot;}}' https://kubernetes:6443/apis/sdsendpoints.net/v1/namespaces/sds-test/sdsendpoints/ --insecure
</code></pre>
<p>And curl had no problem successfully posting to the kubernetes API server (and using the JSON output generated by boost property tree no less).</p>
<p>So right now the focus is clearly on some kind of change in boost::beast between 1.67.0 and 1.75.0. I'm left absolutely scratching my head here wondering if there is some kind of new regression that was introduced in 1.75.0...</p>
<p>I have tried two different compilers for this code: GCC 4.8.5 and Intel icpc 19.1.0.166 20191121. The code is being compiled and run on RHEL 7.9.</p>

stix
<p>The problem, as Yuri pointed out, turned out to be due to a spurious newline at the end of the bearer token file that was being read in.</p>
