curl with variables on bash script

When I use actual values on curl command in the next script, it’s working and giving me a result back, but when I use variables it does not work properly

I think this is issue with how I define them on the command

amz_t=$(cat amazon-token.txt )
flx_id=$(cat flex-id.txt )
ses_t=$(cat session-token.txt )

curl -s -H 'Host: flex-capacity-na.amazon.com' 
  -H 'Cookie: session-token='$ses_t'' 
  -H 'x-amz-access-token: '$amz_t'' 
  -H 'x-flex-instance-id: '$flx_id'' 
  -H 'Accept: */*' 
  -H 'User-Agent: iOS/10.2.2 (iPhone Darwin) Model/iPhone Platform/iPhone6,1 RabbitiOS/2.0.141' 
  -H 'Accept-Language: en-us' 
  --compressed 'https://flex-capacity-na.amazon.com/GetOffersForProvider?serviceAreaIds=122' >> output.txt

This is the command I try to run in script above mention txt files only contain the certain values no garbage values.

Asked By: DarshanaK

||

You can’t use single quotes on your variables. This will cause bash to not interpret the $ special character. You can use double quotes instead.
http://tldp.org/LDP/abs/html/quoting.html

EDIT

I realize now you are closing all your single quotes before and re-opening them after your variables but the variables are still probably being read incorrectly because they aren’t quoted. I’m not sure you even need to single quote everything in this command but if you do you can still put your double quotes in like below:

Change your line:

curl -s -H 'Host: flex-capacity-na.amazon.com' -H 'Cookie: session-token='$ses_t'' -H 'x-amz-access-token: '$amz_t'' -H 'x-flex-instance-id: '$flx_id'' -H 'Accept: */*' -H 'User-Agent: iOS/10.2.2 (iPhone Darwin) Model/iPhone Platform/iPhone6,1 RabbitiOS/2.0.141' -H 'Accept-Language: en-us' --compressed 'https://flex-capacity-na.amazon.com/GetOffersForProvider?serviceAreaIds=122' >> output.txt

To:

curl -s -H 'Host: flex-capacity-na.amazon.com' -H 'Cookie: session-token='"$ses_t" -H 'x-amz-access-token: '"$amz_t" -H 'x-flex-instance-id: '"$flx_id" -H 'Accept: */*' -H 'User-Agent: iOS/10.2.2 (iPhone Darwin) Model/iPhone Platform/iPhone6,1 RabbitiOS/2.0.141' -H 'Accept-Language: en-us' --compressed 'https://flex-capacity-na.amazon.com/GetOffersForProvider?serviceAreaIds=122' >> output.txt

But I think this would work too:

curl -s -H 'Host: flex-capacity-na.amazon.com' -H "Cookie: session-token=$ses_t" -H "x-amz-access-token: $amz_t" -H "x-flex-instance-id: $flx_id" -H 'Accept: */*' -H 'User-Agent: iOS/10.2.2 (iPhone Darwin) Model/iPhone Platform/iPhone6,1 RabbitiOS/2.0.141' -H 'Accept-Language: en-us' --compressed 'https://flex-capacity-na.amazon.com/GetOffersForProvider?serviceAreaIds=122' >> output.txt
Answered By: jesse_b

Try something like this:

amz_t=$(cat amazon-token.txt)
flx_id=$(cat flex-id.txt)
ses_t=$(cat session-token.txt)

UA='iOS/10.2.2 (iPhone Darwin) Model/iPhone Platform/iPhone6,1 RabbitiOS/2.0.141'

URL='https://flex-capacity-na.amazon.com/GetOffersForProvider?serviceAreaIds=122'

curl -s -H 'Host: flex-capacity-na.amazon.com' 
  -H "Cookie: session-token=$ses_t" 
  -H "x-amz-access-token: $amz_t" 
  -H "x-flex-instance-id: $flx_id" 
  -H 'Accept: */*' 
  -H "User-Agent: $UA" 
  -H 'Accept-Language: en-us' 
  --compressed "$URL" >> output.txt

Use single-quotes for fixed strings (i.e. without any variables in them) and double-quotes for strings that need variable interpolation to take place.

Answered By: cas