Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

loop through list of jsons, curl it and save outputs

I have a list of jsons in a file input.dat like so (many are utf-8 encoded):

{"var1": "laptop", "var2": "new", "var3": "ugly"}
{"var1": "televison", "var2": "old", "var3": "bad"}
{"var1": "cleaner", "var2": "used", "var3": "good"}
{"var1": "Pakman", "var2": "sale", "var3": "thrones"}

I have built an api which works with the following curl command:

curl --header "Content-Type: application/json" \
-H 'Authorization: Token 878768' \
--request POST \
--data '{"var1":"test", "var2":"item", "var3":"stack"}' \
"http://<ip_add>:80/endpoint/api/" -o out.dat

Id like to loop through all the jsons line by line in the input file and send it to the above curl and save it as separate outputs – taking care that utf-8 is also correctly encoded.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Not exactly sure what is the best way to do this. I think I need to cat the json, pipe it to curl:

cat input.dat | curl? -o <outdir/out?>

I thought this would be a common problem but I coudnt find a suitable answer on stack 🙁

>Solution :

Read the file line by line and call curl.

while read -r json
do
    curl --header "Content-Type: application/json" \
    -H 'Authorization: Token 878768' \
    --request POST \
    --data "$json" \
    "http://<ip_add>:80/endpoint/api/"
done < input.dat > out.dat
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading