Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

curl command can not be formed dynamically due to single quote

I want to have a curl command like below

curl --location --request POST 'https://abcd.com/api/v4/projects/<projectId>/triggers' \
--header 'PRIVATE-TOKEN: <your_access_token>' \
--form 'description="my description"'

Now I wrote a shell script function to generate it dynamically

it need projectId, token, and description as a pramter

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

callApi(){
while IFS="," read -r -a users; do
for u in "${users[@]}"
do
url="'https://abcd.com/api/v4/projects/$1/triggers'"
echo $url

header="'PRIVATE-TOKEN: $2'"
echo $header

desc="'description=$u token'"
echo $desc

tk=$(curl --location --request POST $url \
--header $header \
--form $desc)

echo $tk

done
done <<< $(cat $3)
}


callApi "<projectId>" "<token>" ./users.csv

It echo perfectly
But
It thorws error

>Solution :

Don’t use both double and single quotes like that. You are adding literal single quotes to the url (and other variables) which, as you have discovered, breaks.

Use double quotes if you need to allow command or parameter substitution, single quotes otherwise. Double quote your variables everywhere you dereference them.

Use indentation for readability.

Useless use of cat.

callApi() {
  while IFS="," read -r -a users; do
    for u in "${users[@]}"; do
      url='https://abcd.com/api/v4/projects/$1/triggers'
      echo "$url"

      header="PRIVATE-TOKEN: $2"
      echo "$header"

      desc="description=$u token"
      echo "$desc"

      tk=$(
        curl --location \
             --request POST \
             --header "$header" \
             --form "$desc" \
             "$url"
      )
      echo "$tk"
    done
  done < "$3"
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading