using variables inside the curl command not working in bash script

7,419

Your issue is mainly with the -F flags in the string $final_paths. It is being passed a a single argument to curl. The solution is not to un-quote the variable expansion to rely on the shell splitting the string properly.

When you have a list of things that you need to pass to a program as separate items, use an array:

#!/bin/bash

url='http://localhost:4502'
url+='/content/test/events/whats-on/all-about-women-home/2018/wine-tasting/jcr:content'

tag_paths=(
    '/content/cq:tags/sales-stage/pre-sale'
    '/content/cq:tags/sales-stage/special-offer'
)

curl_opts=( --user "admin:admin" --form "tags3@TypeHint=String[]" )

for tag_path in "${tag_paths[@]}"; do
    curl_opts+=( --form "tags2=$tag_path" )
done

curl "${curl_opts[@]}" "$url"

Here, we put the options to pass to curl in the array curl_opts. We initiate this array with the things we know will always be there and then add the tag path options by iterating over the array tag_paths. The double-quoted expansion "${curl_opts[@]}" at the end will expand to all elements of the curl_opts array, with each element individually quoted.

I've also chosen to build the complete URL at the start as it is static, and I'm using the long option for curl as this is a script and we can afford to be a bit more verbose (for the sake of readability).

Doing it this way, the quoting becomes intuitive and you don't need to bother with parsing comma-separated lists, with escaping special characters, or setting IFS to some non-default value.


The same script, but for /bin/sh:

#!/bin/sh

url='http://localhost:4502'
url="$url/content/test/events/whats-on/all-about-women-home/2018/wine-tasting/jcr:content"

set -- \
    '/content/cq:tags/sales-stage/pre-sale' \
    '/content/cq:tags/sales-stage/special-offer'

for tag_path do
    set -- "$@" --form "tags2=$tag_path"
    shift
done

set -- --user "admin:admin" --form "tags3@TypeHint=String[]" "$@"

curl "$@" "$url"

Here, we are restricted to using only one array, $@. Elements are set in this array with set.

Share:
7,419

Related videos on Youtube

Komatsu Kai
Author by

Komatsu Kai

Updated on September 18, 2022

Comments

  • Komatsu Kai
    Komatsu Kai over 1 year

    I am trying to write a bash script to update certain nodes in my repository. I wrote below script, but it does not seem to be working when I use variables inside curl. Below is the code. I tried all possible combinations using "" inside curl statement to resolve the variable. But it does not seem to update the nodes. (I don't get any error when running the script).

    I echo'ed the curl line like:

    echo "curl --user admin:admin "$final_add" http://localhost:4502"$a""
    

    and placed its output in the script, then the script runs fine and updated the node.

    Can anyone provide me some guidance on why cant i update the nodes using the variables in curl.

    Code Sample Below

    #!/bin/bash
    
    echo "-------------------------------------------------------------------------------------------------------------------"
    echo "Script to set tags"
    echo "-------------------------------------------------------------------------------------------------------------------"
    
    
    
    if [ true ]
    then
        echo "**firing curl command for tags2**"
    
        a="/content/test/events/whats-on/all-about-women-home/2018/wine-tasting/jcr:content"
        i="[/content/cq:tags/sales-stage/pre-sale,/content/cq:tags/sales-stage/special-offer]"
        str=$i
        IFS=,
        ary=($str)
    
        for key in "${!ary[@]}"; do tags_paths+="-Ftags2=${ary[$key]} "; done 
    
        final_paths=$(echo $tags_paths | sed "s|[2],]||g")
    
        final_add="-Ftags2@TypeHint=\"String[]\" ${final_paths//[[[\[\]]/}"
    
        #have tried this without quotes too --eg : (curl --user admin:admin  $final_add http://localhost:4502$a) it too didnt work
        curl --user admin:admin  "$final_add" http://localhost:4502"$a"
    fi
    
    • nohillside
      nohillside over 5 years
      With the script as written, the content of "$final_add" is passed to curl as one argument, containing -Ftags2@TypeHint="String[]" -Ftags2=/content/cq:tags/sales-stage/pre-sale -Ftags2=/content/cq:tags/sales-stage/special-offer. Is this what you want?
    • Komatsu Kai
      Komatsu Kai over 5 years
      I need a command like this: (curl --user admin:admin -Ftags3@TypeHint="String[]" -Ftags3=/content/cq:tags/sales-stage/pre-sale -Ftags3=/content/cq:tags/sales-stage/special-offer localhost:4502/content/something/events/whats-on/…) does my $final_add ned to be changed for this?
    • 林果皞
      林果皞 over 5 years
      You can use curl -v option to know the network request details.