1

I want to write a generic function in cpp that gets JSON data using cpprestsdk and copy the http status response code and the JSON data. The calling method will use the json_resp and http_status codes. Later on, I want to further make this function more generic by passing the URL and use it to get data from different web services. Please let me know how I can accomplish this.

pplx::task<void> handleWebServerRequest( web::json::value json_resp, int *http_status)
{
..
http_client client(L"http://weburl.com:8000/getjsondata");

return client.request(methods::GET).then([](http_response response) -> pplx::task<json::value> {
// Store the http status code to be returned to calling function
*http_status = response.status_code();
..
if(response.status_code() == status_codes::OK) {
   return response.extract_json();
}

return pplx::task_from_result(json::value()); }).then([](pplx::task<json::value> previousTask) {
   try {
        // capture json response to json_resp
        json_resp = previousTask.get();
    }
    catch( const http_exception& e) {
         // print error
    }
});
}
halfer
  • 19,824
  • 17
  • 99
  • 186
QueryMan
  • 21
  • 3

1 Answers1

0

In my research I have found that the only difference between using cpprest api to consume a PHP web service and a WCF web service is the function parameter. When consuming a PHP web service you can set the function parameter to an empty string. Where as when consuming a WCF service you need to pass it a function parameter-because the protocol for receiving requests and issuing responses in a WCF service is very different, but the process of sending requests and receiving responses is asynchronous so there will always be at least three modules, functions or tasks involved. One to make the request. The other to wait and receive the response and another to parse the data which is called asynchronously by the function that receives the response. I suppose you could put all three tasks into one function and use go to statements to execute each task, perhaps use some inline assembly to capture the response, and use pointers in place of parameters - but it is still three tasks anyway you slice it. The two others run in a thread and do not have access to the application data, but the last function that parses the data (the json object) which is called asynchronously you could make generic. I don't know which web services you want to consume, but I posted two samples on github-Example of Casablanca (cpprestsdk 2.9.1) consuming a PHP web service and Example of Casablanca (cpprestsdk 2.9.1) consuming a WCF (.net) web service. I believe this should get you off to a good start. To capture the json values you can convert your json values to std strings (as shown below) and then you can store them respectively in a local hashmap by adding a hashmap pointer argument to all three functions and passing a reference to the local hashmap variable from which ever function you are calling it from where they can be converted to what ever data type you need.

 void get_field_map_json(json::value & jvalue, unordered_map <string, string>  * hashmap)
 {

  if (!jvalue.is_null())
   {
    for (auto const & e : jvalue.as_object())
    {
        std::string key(conversions::to_utf8string(e.first));
        std::string value(conversions::to_utf8string(e.second.as_string()));
        (*hashmap)[key] = value;



    }

}
IjonTichy
  • 96
  • 1
  • 1
  • 10