title: Read JSON data from http request using boost asio
body: <p>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. </p>
  
  <p>I have already looked at: </p>
  
  <p><a href="https://stackoverflow.com/questions/17799229/boost-asio-post-http-request-headers-and-body">BOOST ASIO POST HTTP REQUEST -- headers and body</a></p>
  
  <p><a href="https://stackoverflow.com/questions/26761058/how-to-send-http-request-and-retrieve-a-json-response-c-boost">How to send http request and retrieve a json response C++ Boost</a></p>
  
  <p><a href="https://stackoverflow.com/questions/19562786/http-post-request-using-boostasio">HTTP POST request using boost::asio</a></p>
  
  <p><a href="https://stackoverflow.com/questions/36141746/boost-asio-http-client-post">Boost ASIO HTTP client POST</a></p>
  
  <p>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: <a href="http://www.boost.org/doc/libs/1_51_0/doc/html/boost_asio/example/http/server/" rel="nofollow noreferrer">http://www.boost.org/doc/libs/1_51_0/doc/html/boost_asio/example/http/server/</a></p>
  
  <p>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). </p>
  
  <pre><code>void request_handler::handle_request(const request&amp; req, reply&amp; 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&lt;string&gt; 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 &amp;&amp; last_dot_pos &gt; 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() &gt; 0)
      rep.content.append(buf, is.gcount());
    rep.headers.resize(2);
    rep.headers[0].name = "Content-Length";
    rep.headers[0].value = boost::lexical_cast&lt;std::string&gt;(rep.content.size());
    rep.headers[1].name = "Content-Type";
    rep.headers[1].value = mime_types::extension_to_type(extension);
  }
  </code></pre>
  
  <p>What I am trying to achieve is from the website, receive parameters for the SQL query for each of the methods: </p>
  
  <ul>
  <li>getAllUsers </li>
  <li>getUser: SELECT * FROM 'table' WHERE user_id=x </li>
  <li>editUser: UPDATE 'table' SET col1='val1,col2='val2',... WHERE id=x</li>
  </ul>
  
  <p>Make the database call, and send a response in a JSON format.</p>
  
  <p>Any help would be appreciated!</p>