cURL / Mailing Lists / curl-library / Single Mail

curl-library

Send Remote Source in JSON body

From: Robert Hudspeth <hudspeth_at_bigml.com>
Date: Tue, 28 Jul 2015 11:45:30 -0700

Greetings. My name is Robert Hudspeth and I am a summer intern at BigML.

I have a remote .csv file that I need to POST to our REST API in a JSON
body, yet the code I have doesn't quite work. I believe what's happening is
that the JSON body isn't in the right format that the API accepts, so it's
not going in properly. How can I modify what's within the JSON body through
libcURL? I've browsed the examples, tutorials, and mailing-list pages, and
I wasn't able to find anything that could help.

Thank you very much for your time.

CODE:

curl_global_init(CURL_GLOBAL_ALL);

curl = curl_easy_init();

if(curl) {

     struct curl_slist *slist = NULL;

     slist = curl_slist_append(slist, "Content-Type: application/json");

     curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);

     curl_easy_setopt(curl, CURLOPT_POSTFIELDS, name.data()); //Link to
.csv file

     curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(name.data()));

     curl_easy_setopt(curl, CURLOPT_URL, request.data()); //URL to be
POSTed to

     result = curl_easy_perform(curl);

     if(result != CURLE_OK)

          cout << "Nothing was returned." << endl;}

     else {

          cout << "cURL failed to connect. Please check your connections"
<< endl;}

     curl_easy_cleanup(curl);

     cout << endl;

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2015-07-28