shuba.ivan
shuba.ivan

Reputation: 4061

Bash curl how to set up variable in header?

I faced with problem when created simple bash script, so, my goal - fect jwt token and will use tokn in next query, For that first curl fetch credentials from login.json file and then set it in result.json, then reed token key and set it in variable tokenValue then I want to set it on bext curl in header but when I debugged I did not get Authorization header, why ? When I substitute the variable manually (like "Authorization: Bearer test_token") - it's worked, I faced with HTTP_AUTHORIZATION=Bearer test_token

curl --request POST -sL \
     --url 'http://auth.loc/api/login'\
     --output './result.json' \
     --header "Content-Type: application/json" \
     -d "@login.json" \

tokenValue=$(jq -r '.token' './result.json')
bearerToken="Authorization: Bearer "$tokenValue
echo 'fff' + $bearerToken

curl --request GET -sL \
     --url 'http://qbee.local/api/v2/grouptree?_format=json'\
     --output './grouptree_result.json' \
     --header $bearerToken \

echo "$(cat grouptree_result.json)"

echo 'fff' + $bearerToken works fine, I faced with correct data in cli

Upvotes: 0

Views: 1127

Answers (1)

shuba.ivan
shuba.ivan

Reputation: 4061

I found out how to resolve it. Problem was with double and single quotes

curl --request POST -sL \
 --url 'http://qbee.local/api/v2/login'\
 --output './result.json' \
 --header "Content-Type: application/json" \
 -d "@login.json" \

tokenValue=$(jq -r '.token' './result.json')

echo 'token was' + $tokenValue

curl --request GET -sL \
     --url 'http://qbee.local/api/v2/metric/1587260952123_10?type=group'\
     --output './metric_result.csv' \
     --header 'Authorization: Bearer '"$tokenValue" \
     --header 'Content-Type: csv' \

echo "$(cat grouptree_result.json)"

Upvotes: 1

Related Questions