The best way to add Unix Shell variables to CURL GET request

19,536

You don't seem to be escaping special characters properly; specifically ;, & and ?

Also, it may be a function of this website, but your double quotes aren't the quotes you're looking for: isn't the same as "

I'd use:

# curl "${WEBSITE_URL}/${ACTION_NAME}/${ACTION1}\;id=${ID}\?Command=TEST\&id1=${ID1}\&id2=${ID2}\&id3=${ID3}"

Share:
19,536

Related videos on Youtube

C_U
Author by

C_U

Updated on September 18, 2022

Comments

  • C_U
    C_U over 1 year

    Let’s say I use curl –X GET command with the following link:

    “$WEBSITE_URL/ACTION_NAME/ACTION1;id=$ID?Command=TEST&id1=$ID1&id2=$ID2&id3=$ID3”
    

    As you can see, there are some $variables in this string. Obviously, just adding them to the string without letting Bash know where these variables start and end won’t work.

    So I tried to use all kinds of tags that I found (i.e. ‘”$Variable”’, ${Variable}, etc.) to separate variables and make this string look and act more like a normal request, but none of them worked. There were some related topics here, they were helpful, but information I got still didn’t make it work – probably because my case is a bit different from basic examples and I’m missing something here. Any ideas? Cheers!

    • Stéphane Chazelas
      Stéphane Chazelas over 9 years
      Are you sure about that ;id=$ID before the query part (?...)?
    • Hauke Laging
      Hauke Laging over 9 years
      Explain exactly what the problem is i.e. what should happen and what happens instead?
    • Stéphane Chazelas
      Stéphane Chazelas over 9 years
      Why do you want to use -X GET (as opposed to letting curl forget the GET request properly)?
  • Stéphane Chazelas
    Stéphane Chazelas over 9 years
    Strictly speaking $ introduces parameter expansion, variables being one type of parameters. For instance, 3, and # are not valid variable names but $3 and $# are valid parameter expansions. Also note that $12 is ${1}2 (in bash/ksh/yash/bourne, but not zsh/ash).
  • Stéphane Chazelas
    Stéphane Chazelas over 9 years
    That ; needs to be quoted or otherwise it will be considered as a variable separator. Using a variable won't really help here especially if you forget to quote it.
  • YoMismo
    YoMismo over 9 years
    You're right, thanks for the comment. I'll modify the answer adding the missing quotes.