Is there any way to have parameters in a file instead doing like
curl -X GET -G 'http://rest_url' -d 'param1=value1' -d 'param2=value2' -d 'paramX=valueX'
to have
curl -X GET -G 'http://rest_url' -d @file.txt
I have moved to that file as
'param1=value1'
'param2=value2'
'paramX=valueX'
but that doesn't work.
Yes. You can do them in a file, but then they will appear as a single -d option so curl won't insert the ampersand between the parameters, so you'd have to write the file to contain the data like this:
param1=value1¶m2=value2
curl -G 'http://rest_url' -d @file.txt
If you rather keep the parameters separated with newlines as in the question, I would suggest a small shell script to fix that:
#!/bin/sh
while read param; do
args="$args -d $param";
done
curl -G $args http://example.com
and use it like this:
script.sh < file.txt
As @daniel-stenbeng wrote, the key is to use -G
to force a GET request and a single -d
for all query string parameters. A way to avoid writing another shell script is to make -d
read from STDIN (-d @-
) and then feed STDIN with the joined string of parameters.
An easy way to do this is joining is with paste
command:
paste -s -d'&' file.txt | curl -G http://example.com -d @-
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With