I\'ve been trying to get this to work for a couple of days however I keep getting a 400 error from the server.
Basically, what I\'m trying to do is send a http POST
Although this question is very old I would like to post this answer for users who are facing similar problem for http POST.
The server is sending you HTTP 400 means "BAD REQUEST". It is because the way you are forming your request is bit wrong.
The following is the correct way to send the 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\r\n";
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 are sending any data(json,string etc) with your POST request, make sure:
(1) Content-Length: is accurate.
(2) that you put the Data at the end of your request with a line gap.
(3) and for that (2nd point) to happen you MUST provide double line feed (i.e. \r\n\r\n) in the last header of your header request. This tells the header that HTTP request content is over and now it(server) will get the data.
If you don't do this then the server fails to understand that where the header is ending ? and where the data is beginning ? So, it keeps waiting for the promised data (it hangs).
Disclaimer: Feel free to edit for inaccuracies, if any.