Issue
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.
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
.
i=1
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/" > out.$i.dat
((i++))
done < input.dat
Answered By - Barmar Answer Checked By - Gilberto Lyons (WPSolving Admin)