title: BOOST ASIO POST HTTP REQUEST -- headers and body
body: <p>I've been trying to get this to work for a couple of days however I keep getting a 400 error from the server.</p>
  
  <p>Basically, what I'm trying to do is send a http POST request to a server that requires a JSON request body with a couple of properties. </p>
  
  <p>These are the libs I'm currently using</p>
  
  <p>UPDATED --- 7/23/13 10:00am just noticed I'm using TCP instead of HTTP not sure how much this will effect an HTTP call but i can't find any examples of clients using pure HTTP with BOOST::ASIO</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;istream&gt;
  #include &lt;ostream&gt;
  #include &lt;string&gt;
  #include &lt;boost/asio.hpp&gt;
  
  #include &lt;sstream&gt;
  #include &lt;boost/property_tree/ptree.hpp&gt;
  #include &lt;boost/property_tree/json_parser.hpp&gt;
  
  using boost::property_tree::ptree; using boost::property_tree::read_json; using boost::property_tree::write_json;
  
  using boost::asio::ip::tcp;
  </code></pre>
  
  <p>SET UP CODE</p>
  
  <pre><code>    // Get a list of endpoints corresponding to the server name.
  tcp::resolver resolver(io_service);
  tcp::resolver::query query(part1, "http");
  tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
  
  // Try each endpoint until we successfully establish a connection.
  tcp::socket socket(io_service);
  boost::asio::connect(socket, endpoint_iterator);
  
  // Form the request. We specify the "Connection: close" header so that the
  // server will close the socket after transmitting the response. This will
  // allow us to treat all data up until the EOF as the content.
  boost::asio::streambuf request;
  std::ostream request_stream(&amp;request);
  </code></pre>
  
  <p>JSON BODY</p>
  
  <pre><code>ptree root, info;
  root.put ("some value", "8");
  root.put ( "message", "value value: value!");
  info.put("placeholder", "value");
  info.put("value", "daf!");
  info.put("module", "value");
  root.put_child("exception", info);
  
  std::ostringstream buf; 
  write_json (buf, root, false);
  std::string json = buf.str();
  </code></pre>
  
  <p>HEADER AND CONNECTION REQUEST</p>
  
  <pre><code>request_stream &lt;&lt; "POST /title/ HTTP/1.1 \r\n";
  request_stream &lt;&lt; "Host:" &lt;&lt; some_host &lt;&lt; "\r\n";
  request_stream &lt;&lt; "User-Agent: C/1.0";
  request_stream &lt;&lt; "Content-Type: application/json; charset=utf-8 \r\n";
  request_stream &lt;&lt; json &lt;&lt; "\r\n";
  request_stream &lt;&lt; "Accept: */*\r\n";    
  request_stream &lt;&lt; "Connection: close\r\n\r\n";
  
  // Send the request.
  boost::asio::write(socket, request);
  </code></pre>
  
  <p>I put place holder values however if you see anything that doesn't work in my code that jumps out please let me know I have no idea why i keep getting a 400, bad request.</p>
  
  <p>info about the rig</p>
  
  <p>C++</p>
  
  <p>WIN7</p>
  
  <p>VISUAL STUDIO</p>