Search code examples
curlparametersget

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.


Solution

  • 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