cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: Send Remote Source in JSON body

From: Aaron Meriwether <me_at_ameriwether.com>
Date: Tue, 28 Jul 2015 14:12:57 -0600

> On Jul 28, 2015, at 12:45 PM, Robert Hudspeth <hudspeth_at_bigml.com> wrote:
>
> 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.

Just a guess as to your problem, but CSV != JSON. You probably need to parse the data out of the CSV file and build a JSON document from it according to the specifications of your API, then submit that JSON document. Even if your JSON API expects a POST containing block of raw CSV data, that block will need to at the very least be escaped, quoted, and embedded into a JSON skeleton. (e.g. {"my_csv_data":"aa,ba,ca\nab,bb,cb\nac,bc,cc"} )

You should probably experiment using the curl command-line tool first until you get the API format right before trying to use libcurl.

Here is an example of doing a JSON post via the command-line:
http://stackoverflow.com/questions/7172784/how-to-post-json-data-with-curl-from-terminal-commandline-to-test-spring-rest <http://stackoverflow.com/questions/7172784/how-to-post-json-data-with-curl-from-terminal-commandline-to-test-spring-rest>

If you need a starting-point for learning more about the JSON format in particular, this is the definitive resource:
http://json.org <http://json.org/>

-Aaron

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