Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cURL GET request and parameters in a file

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.

like image 370
JackTheKnife Avatar asked Sep 04 '25 17:09

JackTheKnife


2 Answers

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&param2=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
like image 109
Daniel Stenberg Avatar answered Sep 07 '25 15:09

Daniel Stenberg


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 @-
like image 31
Michail Alexakis Avatar answered Sep 07 '25 16:09

Michail Alexakis