The best way to add Unix Shell variables to CURL GET request
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}"
Related videos on Youtube
C_U
Updated on September 18, 2022Comments
-
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 over 9 yearsAre you sure about that
;id=$ID
before the query part (?...
)? -
Hauke Laging over 9 yearsExplain exactly what the problem is i.e. what should happen and what happens instead?
-
Stéphane Chazelas over 9 yearsWhy do you want to use
-X GET
(as opposed to lettingcurl
forget the GET request properly)?
-
-
Stéphane Chazelas over 9 yearsStrictly 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
(inbash
/ksh
/yash
/bourne
, but notzsh
/ash
). -
Stéphane Chazelas over 9 yearsThat
;
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 over 9 yearsYou're right, thanks for the comment. I'll modify the answer adding the missing quotes.