I need to call a cool webservice that accepts JSN objects. I was able to find libraries such as libcurl to call the webservice from C application, I was able to find libraries like json-c to create a Jason object in C Was there.
libcurl accepts a string and posts it in the given webservice url Is there anyway that I can post a Jason object using json-c libraries and using curl libraries I can call the webservice
Please tell me that there is another library which will allow me both to create a Jason object and call a webservice or any other alternative solution.
Thanks for the help in advance. Here is an example using both libraries:
#include & lt; Stdio.h & gt; # Include & lt; Curl / curl.h & gt; # Include & lt; Json-c / json.h & gt; Int main (int argc, char ** argv) {/ * LIB JSON-C part * / / * Create a JSON object * / json_object * jObj = json_object_new_object (); / * Create a string element * / json_object * jString = json_object_new_string ("cow"); / * Add Element to the JSON Ultimate Object * / json_object_object_add (jObj, "name", jString); / * LIB curl part * / / * start curl * / curl * curlHandler = curl_easy_init (); If (curlhandler) {/ * set the curl parameter / curl_easy_setopt (curlhandler, curlop_your URL, "http://api.yoururl.com"); Curl_easy_setopt (curl handler, CURLOPT_CUSTOMREQUEST, "post"); Curl_easy_setopt (Carlhandsler, Curlop T_POSTFIELDS, json_object_to_json_string (jObj)); / * Request * / cURLcode res = curl_easy_perform (curlHandler); / * Checking errors * / if (res! = CURLE_OK) fprintf (stderr, "curl fail:% s \ n", curl_easy_strerror (res)); / * Clean up * / curl_ac_column (curlhandler); Json_object_object_del (jObj, "name"); Free (jObj); } Return 0; }
Comments
Post a Comment