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
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