Home > Enterprise >  Bearer token as variable in curl
Bearer token as variable in curl

Time:03-18

I'm trying to pass variable for bearer token, I fail do forge my request. Basically if I add the token manually it works.

curl ... --header "Authorization: Bearer MmM2MDZkMDA0MjE0" --verbose
* About to connect()
* Connected to ...
* ...
> Authorization: Bearer MmM2MDZkMDA0MjE0
< HTTP/1.1 200 OK
<...
    response header and body was here
...
* Connection #0 to host host.local left intact

But when I try to assign it to a variable, it's not working

$bearerToken='MmM2MDZkMDA0MjE0'
curl ... --header "Authorization: Bearer $bearerToken" --verbose
* About to connect()
* Connected to ...
* ...
> Authorization: Bearer MmM2MDZkMDA0MjE0
< HTTP/1.1 400 Bad EOL
< Content-Length: 0
< Connection: close
<
* Closing connection 0

I even try with creating variable like the one bellow and pass it, but I had the same output

testToken='Authorization: Bearer '$bearerToken
curl ... --header $testToken

What I'm doing wrong?

CodePudding user response:

What I found is that curl is broken because when using variables they include new line at the end. What worked for me is using:

"Authorization: Bearer ${bearerToken//[$'\t\r\n ']}"

  •  Tags:  
  • curl
  • Related