I am trying to receive JSON data from an HTTP post request using Boost ASIO on the c++ server side. I am able to handle normal HTTP requests however I'm clueless on how to read the JSON data being sent to the server.
I have already looked at:
BOOST ASIO POST HTTP REQUEST -- headers and body
How to send http request and retrieve a json response C++ Boost
HTTP POST request using boost::asio
but I dont quite understand them as to where to read the specific JSON post at handle_request method. I have the Boost ASIO server running from the tutorials on the Boost documentation online: http://www.boost.org/doc/libs/1_51_0/doc/html/boost_asio/example/http/server/
As I am new to network programming, please help me out on how reading JSON data from the front end HTTP request (I have figured out how to parse JSON data using Boost Property Tree).
void request_handler::handle_request(const request& req, reply& rep)
{
// Decode url to path.
std::string request_path;
if (!url_decode(req.uri, request_path))
{
rep = reply::stock_reply(reply::bad_request);
return;
}
// Request path must be absolute and not contain "..".
if (request_path.empty() || request_path[0] != '/'
|| request_path.find("..") != std::string::npos)
{
rep = reply::stock_reply(reply::bad_request);
return;
}
vector<string> tokens=Utils::tokenizeString(request_path,"/");
//token[0] is always request method
if (tokens.size() == 0)
{
request_path += "index.html";
}
else if (tokens[0] == "getAllUsers")
{
rep = reply::stock_reply(reply::ok);
return;
}
else if (tokens[0] == "getUser")
{
//send ok response
rep = reply::stock_reply(reply::ok);
return;
}
else if (tokens[0] == "editUser")
{
//send ok response
rep = reply::stock_reply(reply::ok);
return;
}
// Determine the file extension.
std::size_t last_slash_pos = request_path.find_last_of("/");
std::size_t last_dot_pos = request_path.find_last_of(".");
std::string extension;
if (last_dot_pos != std::string::npos && last_dot_pos > last_slash_pos)
{
extension = request_path.substr(last_dot_pos + 1);
}
// Open the file to send back.
std::string full_path = doc_root_ + request_path;
std::ifstream is(full_path.c_str(), std::ios::in | std::ios::binary);
if (!is)
{
printf("file not found at %s\n",full_path.c_str());
rep = reply::stock_reply(reply::not_found);
return;
}
// Fill out the reply to be sent to the client.
rep.status = reply::ok;
char buf[512];
while (is.read(buf, sizeof(buf)).gcount() > 0)
rep.content.append(buf, is.gcount());
rep.headers.resize(2);
rep.headers[0].name = "Content-Length";
rep.headers[0].value = boost::lexical_cast<std::string>(rep.content.size());
rep.headers[1].name = "Content-Type";
rep.headers[1].value = mime_types::extension_to_type(extension);
}
What I am trying to achieve is from the website, receive parameters for the SQL query for each of the methods:
- getAllUsers
- getUser: SELECT * FROM 'table' WHERE user_id=x
- editUser: UPDATE 'table' SET col1='val1,col2='val2',... WHERE id=x
Make the database call, and send a response in a JSON format.
Any help would be appreciated!