0

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

Boost ASIO HTTP client POST

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!

j_0101
  • 157
  • 4
  • 14
  • Any particular reason this needs to be Boost? Any reason to not use Boost Beast, RestC++, gRpc, etc.? – sehe May 28 '17 at 14:08
  • I am using boost for a lot of related functionality and I particularly like the library, so I thought might as well stick to Boost for the server as well, no other technical reason – j_0101 May 28 '17 at 14:15
  • It sounds like you're underestimating the effort. The cost of suboptimal design choices will mount, and you _will_ make suboptimal design choices since you're "new to network programming". At the very least look at how existing libraries do it. Note that cppnetlib/beast are practically boost (or have been in review for some time) – sehe May 28 '17 at 14:17
  • For now, I don't think "answering" your direct questions by hacking it into the snippets shown is _helping_ you, quite the opposite. And "doing it right" is reinventing wheels, or writing a library for you. That's not how SO works. – sehe May 28 '17 at 14:18
  • so what libraries do you recommend for running a server on c++ that would help in what I am trying to achieve? – j_0101 May 28 '17 at 14:22

0 Answers0