0

I have a couple of curl commands, one to fetch an authentication key and then another that needs to use that key, currently i have the below bash script.

#!/bin/bash

ACCESS_TOKEN=$(curl -XPOST -H "Content-type: application/json" -d '{"client_id":"XXX", "client_secret":"XXX", "grant_type":"client_credentials"}' 'https://url.com' | ./jq '.access_token')

echo $ACCESS_TOKEN

UPLOADARRAY=$(curl -v -XGET -H 'Authorization: Bearer "$ACCESS_TOKEN"' -H 'client_id:XXX' -H "Content-type:application/json" 'https://otherurl.com' | ./jq '{authCode: .authCode, name: .uploadUrl}')

echo $UPLOADARRAY

The issue is looking at the verbose output of the second curl command i see:

> User-Agent: curl/7.77.0
> Accept: */*
> Authorization: Bearer "$ACCESS_TOKEN"

So the content of the variable access content (which the test echo statement shows does contain the right value) isn't being used and instead the string "$ACCESS_TOKEN" is used.

I did have a look at Quoting within $(command substitution) in Bash and wrapping the command with the additional double quotes but this made no difference.

Can anyone spot whats wrong here?

1
  • 1
    You probably just need "Authorization: Bearer $ACCESS_TOKEN"
    – muru
    Feb 17, 2022 at 15:11

1 Answer 1

0

This is the key part:

'Authorization: Bearer "$ACCESS_TOKEN"'

The problem you have is that the outer single quotes prevent variable expansion.

This should allow it to expand successfully, while maintaining good quoting practices:

"Authorization: Bearer \"$ACCESS_TOKEN\""

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .