BOOST ASIO POST HTTP REQUEST - headers and body

I tried to get this to work for a couple of days, but I always get a 400 error from the server.

Basically, I am trying to send an http POST request to a server that requires a JSON request body with several properties.

These are the libraries that I am currently using.

UPDATED --- 7/23/13 10:00 just noticed that I use TCP instead of HTTP, not sure how much this will affect the HTTP call, but I can not find examples of clients using pure HTTP with BOOST :: ASIO

#include <iostream> #include <istream> #include <ostream> #include <string> #include <boost/asio.hpp> #include <sstream> #include <boost/property_tree/ptree.hpp> #include <boost/property_tree/json_parser.hpp> using boost::property_tree::ptree; using boost::property_tree::read_json; using boost::property_tree::write_json; using boost::asio::ip::tcp; 

SET UP 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(&request); 

JSON BODY

 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(); 

REQUEST FOR TITLES AND CONNECTIONS

 request_stream << "POST /title/ HTTP/1.1 \r\n"; request_stream << "Host:" << some_host << "\r\n"; request_stream << "User-Agent: C/1.0"; request_stream << "Content-Type: application/json; charset=utf-8 \r\n"; request_stream << json << "\r\n"; request_stream << "Accept: */*\r\n"; request_stream << "Connection: close\r\n\r\n"; // Send the request. boost::asio::write(socket, request); 

I am adding place holder values, but if you see something that does not work in my code that jumps out, please let me know, I have no idea why I keep getting the 400th request.

rig information

C ++

WIN7

VISUAL STUDIO

+6
source share
1 answer

Although this question is very old, I would like to post this answer for users facing a similar issue for http POST.

The server sends you an HTTP 400, which means "REQUIRED REQUEST." This is because the way you form your request is erroneous.

The following is the correct way to send a POST request containing JSON data.

 #include<string> //for length() request_stream << "POST /title/ HTTP/1.1 \r\n"; request_stream << "Host:" << some_host << "\r\n"; request_stream << "User-Agent: C/1.0"; request_stream << "Content-Type: application/json; charset=utf-8 \r\n"; request_stream << "Accept: */*\r\n"; request_stream << "Content-Length: " << json.length() << "\r\n"; request_stream << "Connection: close\r\n\r\n"; //NOTE THE Double line feed request_stream << json; 

Whenever you send any data (json, string, etc.) with a POST request, make sure that:

(1) Content-Length: is accurate.

(2) so that you put data at the end of your query with a line break.

(3) , and for this (paragraph 2) you must MUST provide a two-line channel (i.e. \ r \ n \ r \ n) in the last header of your header request. This tells the header that the contents of the HTTP request are complete and now it (the server) will receive the data.

If you do not, the server will not be able to figure out where the header ends. ? and where does the data begin? Thus, he continues to wait for the promised data (he freezes).

Disclaimer: Feel free to edit the inaccuracies, if any.

+10
source

Source: https://habr.com/ru/post/950027/


All Articles