cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: Send Remote Source in JSON body

From: Ray Satiro via curl-library <curl-library_at_cool.haxx.se>
Date: Wed, 29 Jul 2015 00:02:29 -0400

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

PostJSON.c [1] is an example that encodes a JSON object using cJSON [2]
and then sends it via POST using libcurl. Consider using an encoding
library when processing arbitrary data. For CSV parsing in C I'd use
libcsv [3] which is very well done, I used it as the backend for my C++
CSV classes [4] and did a lot of review on it.

> 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

Thanks for this. One answer there references a script wrapper for curl
called resty [5] which I'd never heard of before, but it looks really neat.

[1]: https://gist.github.com/jay/2a6c54cc6da442489561
[2]: http://sourceforge.net/projects/cjson/
[3]: http://sourceforge.net/projects/libcsv/
[4]: https://github.com/jay/CSV
[5]: https://github.com/micha/resty

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