Passing variable to “authorization: bearer” header with Curl and Bash2019 Community Moderator ElectionDrag and drop files with cURLcURL for loop variablecurl outfile variable not working in bash scriptHow do I pipe the output of a curl command to an environment variable and use it in another curl command?Using a variable to execute a curl commandHow to put a variable inside cURL?how to put variable in CURL header using shell script?cURL requests in bashVariable inside a curl ArrayGET via wget or cURL gives a limited response
Why Shazam when there is already Superman?
Angel of Condemnation - Exile creature with second ability
Infinite dials to reset ever?
What is this called? Old film camera viewer?
Is aluminum electrical wire used on aircraft?
What is going on with 'gets(stdin)' on the site coderbyte?
Sklearn 'Seed' Not Working Properly In a Section of Code
How can "mimic phobia" be cured or prevented?
What if a revenant (monster) gains fire resistance?
What will be next at the bottom row and why?
What changes for testers when they are testing in agile environments?
Does an advisor owe his/her student anything? Will an advisor keep a PhD student only out of pity?
What should you do when eye contact makes your subordinate uncomfortable?
Electoral considerations aside, what are potential benefits, for the US, of policy changes proposed by the tweet recognizing Golan annexation?
How do you respond to a colleague from another team when they're wrongly expecting that you'll help them?
How to create ADT in Haskell?
Is this toilet slogan correct usage of the English language?
Temporarily disable WLAN internet access for children, but allow it for adults
Rising and falling intonation
Added a new user on Ubuntu, set password not working?
How to implement a feedback to keep the DC gain at zero for this conceptual passive filter?
What percentage of fillings performed today are done with mercury amalgam?
Is there any references on the tensor product of presentable (1-)categories?
New brakes for 90s road bike
Passing variable to “authorization: bearer” header with Curl and Bash
2019 Community Moderator ElectionDrag and drop files with cURLcURL for loop variablecurl outfile variable not working in bash scriptHow do I pipe the output of a curl command to an environment variable and use it in another curl command?Using a variable to execute a curl commandHow to put a variable inside cURL?how to put variable in CURL header using shell script?cURL requests in bashVariable inside a curl ArrayGET via wget or cURL gives a limited response
I've been trying to make this work for a while now, and I just can't see why it's not working. It's just a simple script that's supposed to return either 200 (if it works) or 400 (if it doesn't). The trouble seems to come when I use variables to substitute values in Curl, example of a working curl:
curl --max-time 10 --write-out '%http_code' --silent --output '/dev/null'
--insecure -X GET https://url -H
**'authorization: bearer iUhYMrTBdx1GG97Bd99YqrbPojInugjTTnqT8TiRjqgZrZYynljfKpgfao'**
--header 'content-type: application/json' --header
"requestid: '999993122349238343493843958345'"
This works as expected and returns 200. When I replace the authorization bearer section in the following way I get a 400:
curl --max-time 10 --write-out '%http_code' --silent --output '/dev/null'
--insecure -X GET https://url -H "authorization: bearer $TOKEN" --header
'content-type: application/json' --header
"requestid: '999993122349238343493843958345'"
I tried literally everything. Using the variable as "$TOKEN", doing "'authorization: bearer $TOKEN'", even something similar to "'"'"'$TOKEN'"'"'". I don't know what else to try. When I remove the silent curl option I see the following error:
400ODY></HTML>ror 400. The request is badly formed.</p>rset=us-ascii"></HEAD>trict.dtd">
I'm lost to why it works with plain text and not variables. Any ideas?
Edit:
Added verbose for both:
> GET /REDACTED-URL HTTP/1.1
> Host: hostname:44301
> User-Agent: curl/7.50.3
> Accept: */*
> authorization: bearer eyJ0eXA700JKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6ImF1bkM2QV9MbHFVZGNYV3Z6UzVGOURQUEFTSSIsImtpZCI6ImF1bkM2QV9MbHFVZGNYV3Z6UzVGOURQUEFTSSJ9.eyJpc3MiOiJodHRwczovL0dQLUlEUzo0NDMwMS9HUC5JRFMiLCJhdWQiOiJodHRwczovL0dQLUlEUzo0NDMwMS9HUC5JRFMvcmVzb3VyY2VzIiwiZXhwIjoxNTUyNDI5MTA1LCJuYmYiOjE1NTI0MjU1MDUsImNsaWVudF9pZCI6Ik1FUkNBRE9MSUJSRUlWUiIsImNsaWVudF9NYXJjYSI6IjEiLCJjbGllbnRfVXN1YUNvZGkiOiJEQk8iLCJjbGllbnRfVmFsaWRhRXN0YWRvQ3VlbnRhIjoiMCIsImNsaWVudF9FbnRpZGFkIjoiMzAiLCJzY29wZSI6IlByZXBhZ2FBcGkifQ.kkCGp3EJ7VkvR75Zt0MV5VRmNVf-jP5dcb7GaZF6nI5KGNUxfx_linht3nUUX4Hw55ZKhfkiUhYMrTBdx1GG97Bd99YqrbPojInugjTTnqT8TiRjqgZrZYynljfKpgfaofoBnK0EWgqsQxcxPNre2PkvxWV-x6AzPl_uVWml1Wo2bjAi37kVklYmjkbBIWe8EcyF90MMR28xcipIo2ULj5HgOizhvokPg1qDEiYetTU2MYJ1fFRVgJ5v7KF0mpUk7zBl9dvFeGT3MPfTN9EED4biqE-zRaC0r8DK7lr1PScfVHqs09sI89PXTN9Nm2lOT6c43CsUy-YhylhjJr6AfIARcFv1o0-gy0nKnWxSBYnt8NgK7rZCfpjFDUfmaYL5IXqirob98L0cWfvnV3iL6R73dNciayRDy6PqZG0sHWhKkggBIOhRqh-7VsY99qCqG76Beih-UqWPcWO_H8-4zvYDslsXMj8ff1Nk76h9pxuy-qAkRapNVAbBojf41JFVVaONqgPCH9Jszq7cpFbX6vbVq18p6hOhDb4BhuTDkT6tB30RXQWaSPqRbRs2h7VYIcuxok6wbrBzGA32hBHiSVSQs0PdheJlVy1EcHy27MJuIj_Vs6bsiq0a4GY-scs4A-68fvzsERfsLLINDrS4C9zY1f6RwfEkDsWlQ1ouBIk
> content-type: application/json
> requestid: '999993245235232323523523523535'
>
< HTTP/1.1 200 OK
< Content-Type: application/json; charset=utf-8
< Server: Microsoft-IIS/10.0
< requestid: '999993245235232323523523523535'
< CorrelationId: 3859af6f-4c8f-438c-bcfe-37ab6ecc0d6b
< RequestId: 9999af6f-4c8f-438c-bcfe-37ab6ecc0d6b
< X-Powered-By: ASP.NET
< Date: Tue, 12 Mar 2019 22:10:39 GMT
< Content-Length: 992
<
{ [992 bytes data]
This works ^
> GET /REDACTED-URL HTTP/1.1
> Host: hostname:44301
> User-Agent: curl/7.50.3
> Accept: */*
> authorization: bearer eyJ0eXA700JKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6ImF1bkM2QV9MbHFVZGNYV3Z6UzVGOURQUEFTSSIsImtpZCI6ImF1bkM2QV9MbHFVZGNYV3Z6UzVGOURQUEFTSSJ9.eyJpc3MiOiJodHRwczovL0dQLUlEUzo0NDMwMS9HUC5JRFMiLCJhdWQiOiJodHRwczovL0dQLUlEUzo0NDMwMS9HUC5JRFMvcmVzb3VyY2VzIiwiZXhwIjoxNTUyNDI5MTA1LCJuYmYiOjE1NTI0MjU1MDUsImNsaWVudF9pZCI6Ik1FUkNBRE9MSUJSRUlWUiIsImNsaWVudF9NYXJjYSI6IjEiLCJjbGllbnRfVXN1YUNvZGkiOiJEQk8iLCJjbGllbnRfVmFsaWRhRXN0YWRvQ3VlbnRhIjoiMCIsImNsaWVudF9FbnRpZGFkIjoiMzAiLCJzY29wZSI6IlByZXBhZ2FBcGkifQ.kkCGp3EJ7VkvR75Zt0MV5VRmNVf-jP5dcb7GaZF6nI5KGNUxfx_linht3nUUX4Hw55ZKhfkiUhYMrTBdx1GG97Bd99YqrbPojInugjTTnqT8TiRjqgZrZYynljfKpgfaofoBnK0EWgqsQxcxPNre2PkvxWV-x6AzPl_uVWml1Wo2bjAi37kVklYmjkbBIWe8EcyF90MMR28xcipIo2ULj5HgOizhvokPg1qDEiYetTU2MYJ1fFRVgJ5v7KF0mpUk7zBl9dvFeGT3MPfTN9EED4biqE-zRaC0r8DK7lr1PScfVHqs09sI89PXTN9Nm2lOT6c43CsUy-YhylhjJr6AfIARcFv1o0-gy0nKnWxSBYnt8NgK7rZCfpjFDUfmaYL5IXqirob98L0cWfvnV3iL6R73dNciayRDy6PqZG0sHWhKkggBIOhRqh-7VsY99qCqG76Beih-UqWPcWO_H8-4zvYDslsXMj8ff1Nk76h9pxuy-qAkRapNVAbBojf41JFVVaONqgPCH9Jszq7cpFbX6vbVq18p6hOhDb4BhuTDkT6tB30RXQWaSPqRbRs2h7VYIcuxok6wbrBzGA32hBHiSVSQs0PdheJlVy1EcHy27MJuIj_Vs6bsiq0a4GY-scs4A-68fvzsERfsLLINDrS4C9zY1f6RwfEkDsWlQ1ouBIk
> content-type: application/json
> requestid: '99999324523523534395600'
>
< HTTP/1.1 400 Bad Request
< Content-Type: text/html; charset=us-ascii
< Server: Microsoft-HTTPAPI/2.0
< Date: Tue, 12 Mar 2019 22:11:42 GMT
< Connection: close
< Content-Length: 311
<
{ [311 bytes data]
This doesn't. Same exact headers.
UPDATE: Having the same issue with wget:
With plain text:
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/10.0
requestid: 9987993245235235345225233335324324
CorrelationId: ee516440-af87-4aab-a5fa-5e563338a0be
RequestId: ee516440-af87-4aab-a5fa-5e563338a0be
X-Powered-By: ASP.NET
Date: Thu, 14 Mar 2019 21:53:16 GMT
Content-Length: 992
With variable:
HTTP/1.1 400 Bad Request
Content-Type: text/html; charset=us-ascii
Server: Microsoft-HTTPAPI/2.0
Date: Thu, 14 Mar 2019 21:54:16 GMT
Connection: close
Content-Length: 311
bash curl
add a comment |
I've been trying to make this work for a while now, and I just can't see why it's not working. It's just a simple script that's supposed to return either 200 (if it works) or 400 (if it doesn't). The trouble seems to come when I use variables to substitute values in Curl, example of a working curl:
curl --max-time 10 --write-out '%http_code' --silent --output '/dev/null'
--insecure -X GET https://url -H
**'authorization: bearer iUhYMrTBdx1GG97Bd99YqrbPojInugjTTnqT8TiRjqgZrZYynljfKpgfao'**
--header 'content-type: application/json' --header
"requestid: '999993122349238343493843958345'"
This works as expected and returns 200. When I replace the authorization bearer section in the following way I get a 400:
curl --max-time 10 --write-out '%http_code' --silent --output '/dev/null'
--insecure -X GET https://url -H "authorization: bearer $TOKEN" --header
'content-type: application/json' --header
"requestid: '999993122349238343493843958345'"
I tried literally everything. Using the variable as "$TOKEN", doing "'authorization: bearer $TOKEN'", even something similar to "'"'"'$TOKEN'"'"'". I don't know what else to try. When I remove the silent curl option I see the following error:
400ODY></HTML>ror 400. The request is badly formed.</p>rset=us-ascii"></HEAD>trict.dtd">
I'm lost to why it works with plain text and not variables. Any ideas?
Edit:
Added verbose for both:
> GET /REDACTED-URL HTTP/1.1
> Host: hostname:44301
> User-Agent: curl/7.50.3
> Accept: */*
> authorization: bearer eyJ0eXA700JKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6ImF1bkM2QV9MbHFVZGNYV3Z6UzVGOURQUEFTSSIsImtpZCI6ImF1bkM2QV9MbHFVZGNYV3Z6UzVGOURQUEFTSSJ9.eyJpc3MiOiJodHRwczovL0dQLUlEUzo0NDMwMS9HUC5JRFMiLCJhdWQiOiJodHRwczovL0dQLUlEUzo0NDMwMS9HUC5JRFMvcmVzb3VyY2VzIiwiZXhwIjoxNTUyNDI5MTA1LCJuYmYiOjE1NTI0MjU1MDUsImNsaWVudF9pZCI6Ik1FUkNBRE9MSUJSRUlWUiIsImNsaWVudF9NYXJjYSI6IjEiLCJjbGllbnRfVXN1YUNvZGkiOiJEQk8iLCJjbGllbnRfVmFsaWRhRXN0YWRvQ3VlbnRhIjoiMCIsImNsaWVudF9FbnRpZGFkIjoiMzAiLCJzY29wZSI6IlByZXBhZ2FBcGkifQ.kkCGp3EJ7VkvR75Zt0MV5VRmNVf-jP5dcb7GaZF6nI5KGNUxfx_linht3nUUX4Hw55ZKhfkiUhYMrTBdx1GG97Bd99YqrbPojInugjTTnqT8TiRjqgZrZYynljfKpgfaofoBnK0EWgqsQxcxPNre2PkvxWV-x6AzPl_uVWml1Wo2bjAi37kVklYmjkbBIWe8EcyF90MMR28xcipIo2ULj5HgOizhvokPg1qDEiYetTU2MYJ1fFRVgJ5v7KF0mpUk7zBl9dvFeGT3MPfTN9EED4biqE-zRaC0r8DK7lr1PScfVHqs09sI89PXTN9Nm2lOT6c43CsUy-YhylhjJr6AfIARcFv1o0-gy0nKnWxSBYnt8NgK7rZCfpjFDUfmaYL5IXqirob98L0cWfvnV3iL6R73dNciayRDy6PqZG0sHWhKkggBIOhRqh-7VsY99qCqG76Beih-UqWPcWO_H8-4zvYDslsXMj8ff1Nk76h9pxuy-qAkRapNVAbBojf41JFVVaONqgPCH9Jszq7cpFbX6vbVq18p6hOhDb4BhuTDkT6tB30RXQWaSPqRbRs2h7VYIcuxok6wbrBzGA32hBHiSVSQs0PdheJlVy1EcHy27MJuIj_Vs6bsiq0a4GY-scs4A-68fvzsERfsLLINDrS4C9zY1f6RwfEkDsWlQ1ouBIk
> content-type: application/json
> requestid: '999993245235232323523523523535'
>
< HTTP/1.1 200 OK
< Content-Type: application/json; charset=utf-8
< Server: Microsoft-IIS/10.0
< requestid: '999993245235232323523523523535'
< CorrelationId: 3859af6f-4c8f-438c-bcfe-37ab6ecc0d6b
< RequestId: 9999af6f-4c8f-438c-bcfe-37ab6ecc0d6b
< X-Powered-By: ASP.NET
< Date: Tue, 12 Mar 2019 22:10:39 GMT
< Content-Length: 992
<
{ [992 bytes data]
This works ^
> GET /REDACTED-URL HTTP/1.1
> Host: hostname:44301
> User-Agent: curl/7.50.3
> Accept: */*
> authorization: bearer eyJ0eXA700JKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6ImF1bkM2QV9MbHFVZGNYV3Z6UzVGOURQUEFTSSIsImtpZCI6ImF1bkM2QV9MbHFVZGNYV3Z6UzVGOURQUEFTSSJ9.eyJpc3MiOiJodHRwczovL0dQLUlEUzo0NDMwMS9HUC5JRFMiLCJhdWQiOiJodHRwczovL0dQLUlEUzo0NDMwMS9HUC5JRFMvcmVzb3VyY2VzIiwiZXhwIjoxNTUyNDI5MTA1LCJuYmYiOjE1NTI0MjU1MDUsImNsaWVudF9pZCI6Ik1FUkNBRE9MSUJSRUlWUiIsImNsaWVudF9NYXJjYSI6IjEiLCJjbGllbnRfVXN1YUNvZGkiOiJEQk8iLCJjbGllbnRfVmFsaWRhRXN0YWRvQ3VlbnRhIjoiMCIsImNsaWVudF9FbnRpZGFkIjoiMzAiLCJzY29wZSI6IlByZXBhZ2FBcGkifQ.kkCGp3EJ7VkvR75Zt0MV5VRmNVf-jP5dcb7GaZF6nI5KGNUxfx_linht3nUUX4Hw55ZKhfkiUhYMrTBdx1GG97Bd99YqrbPojInugjTTnqT8TiRjqgZrZYynljfKpgfaofoBnK0EWgqsQxcxPNre2PkvxWV-x6AzPl_uVWml1Wo2bjAi37kVklYmjkbBIWe8EcyF90MMR28xcipIo2ULj5HgOizhvokPg1qDEiYetTU2MYJ1fFRVgJ5v7KF0mpUk7zBl9dvFeGT3MPfTN9EED4biqE-zRaC0r8DK7lr1PScfVHqs09sI89PXTN9Nm2lOT6c43CsUy-YhylhjJr6AfIARcFv1o0-gy0nKnWxSBYnt8NgK7rZCfpjFDUfmaYL5IXqirob98L0cWfvnV3iL6R73dNciayRDy6PqZG0sHWhKkggBIOhRqh-7VsY99qCqG76Beih-UqWPcWO_H8-4zvYDslsXMj8ff1Nk76h9pxuy-qAkRapNVAbBojf41JFVVaONqgPCH9Jszq7cpFbX6vbVq18p6hOhDb4BhuTDkT6tB30RXQWaSPqRbRs2h7VYIcuxok6wbrBzGA32hBHiSVSQs0PdheJlVy1EcHy27MJuIj_Vs6bsiq0a4GY-scs4A-68fvzsERfsLLINDrS4C9zY1f6RwfEkDsWlQ1ouBIk
> content-type: application/json
> requestid: '99999324523523534395600'
>
< HTTP/1.1 400 Bad Request
< Content-Type: text/html; charset=us-ascii
< Server: Microsoft-HTTPAPI/2.0
< Date: Tue, 12 Mar 2019 22:11:42 GMT
< Connection: close
< Content-Length: 311
<
{ [311 bytes data]
This doesn't. Same exact headers.
UPDATE: Having the same issue with wget:
With plain text:
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/10.0
requestid: 9987993245235235345225233335324324
CorrelationId: ee516440-af87-4aab-a5fa-5e563338a0be
RequestId: ee516440-af87-4aab-a5fa-5e563338a0be
X-Powered-By: ASP.NET
Date: Thu, 14 Mar 2019 21:53:16 GMT
Content-Length: 992
With variable:
HTTP/1.1 400 Bad Request
Content-Type: text/html; charset=us-ascii
Server: Microsoft-HTTPAPI/2.0
Date: Thu, 14 Mar 2019 21:54:16 GMT
Connection: close
Content-Length: 311
bash curl
How did you setTOKEN? If you useset -xand then run thecurlcommand, what does the shell show as the argument to-H?
– ilkkachu
Mar 12 at 21:35
$TOKEN was set just plainly on top of the script as: TOKEN="iUhYMrTBdx1GG97Bd99YqrbPojInugjTTnqT8TiRjqgZrZYynljfKpgfao"
– Rodrigo
Mar 12 at 22:03
well yeah, it looks like it should work, so that's why I asked about whatset -xshows. You are sure that you used double quotes around the"authorization: bearer $TOKEN"as you have posted here, and not single quotes, right?
– ilkkachu
Mar 12 at 22:12
Absolutely sure. I tried all combinations anyway, I updated the post showing that the headers are being passed identically as text, yet when I use the variable it fails.
– Rodrigo
Mar 12 at 22:27
Ok. What about invisible characters like non-breaking spaces or CRLF line endings? Ifcurldoesn't show them specially in the output, you wouldn't see them, but the variable assignment might have one in the end. Though with CRLF, you'd probably have the whole file with those line endings anyway, and then the version that doesn't use a variable wouldn't work either.
– ilkkachu
Mar 13 at 11:36
add a comment |
I've been trying to make this work for a while now, and I just can't see why it's not working. It's just a simple script that's supposed to return either 200 (if it works) or 400 (if it doesn't). The trouble seems to come when I use variables to substitute values in Curl, example of a working curl:
curl --max-time 10 --write-out '%http_code' --silent --output '/dev/null'
--insecure -X GET https://url -H
**'authorization: bearer iUhYMrTBdx1GG97Bd99YqrbPojInugjTTnqT8TiRjqgZrZYynljfKpgfao'**
--header 'content-type: application/json' --header
"requestid: '999993122349238343493843958345'"
This works as expected and returns 200. When I replace the authorization bearer section in the following way I get a 400:
curl --max-time 10 --write-out '%http_code' --silent --output '/dev/null'
--insecure -X GET https://url -H "authorization: bearer $TOKEN" --header
'content-type: application/json' --header
"requestid: '999993122349238343493843958345'"
I tried literally everything. Using the variable as "$TOKEN", doing "'authorization: bearer $TOKEN'", even something similar to "'"'"'$TOKEN'"'"'". I don't know what else to try. When I remove the silent curl option I see the following error:
400ODY></HTML>ror 400. The request is badly formed.</p>rset=us-ascii"></HEAD>trict.dtd">
I'm lost to why it works with plain text and not variables. Any ideas?
Edit:
Added verbose for both:
> GET /REDACTED-URL HTTP/1.1
> Host: hostname:44301
> User-Agent: curl/7.50.3
> Accept: */*
> authorization: bearer eyJ0eXA700JKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6ImF1bkM2QV9MbHFVZGNYV3Z6UzVGOURQUEFTSSIsImtpZCI6ImF1bkM2QV9MbHFVZGNYV3Z6UzVGOURQUEFTSSJ9.eyJpc3MiOiJodHRwczovL0dQLUlEUzo0NDMwMS9HUC5JRFMiLCJhdWQiOiJodHRwczovL0dQLUlEUzo0NDMwMS9HUC5JRFMvcmVzb3VyY2VzIiwiZXhwIjoxNTUyNDI5MTA1LCJuYmYiOjE1NTI0MjU1MDUsImNsaWVudF9pZCI6Ik1FUkNBRE9MSUJSRUlWUiIsImNsaWVudF9NYXJjYSI6IjEiLCJjbGllbnRfVXN1YUNvZGkiOiJEQk8iLCJjbGllbnRfVmFsaWRhRXN0YWRvQ3VlbnRhIjoiMCIsImNsaWVudF9FbnRpZGFkIjoiMzAiLCJzY29wZSI6IlByZXBhZ2FBcGkifQ.kkCGp3EJ7VkvR75Zt0MV5VRmNVf-jP5dcb7GaZF6nI5KGNUxfx_linht3nUUX4Hw55ZKhfkiUhYMrTBdx1GG97Bd99YqrbPojInugjTTnqT8TiRjqgZrZYynljfKpgfaofoBnK0EWgqsQxcxPNre2PkvxWV-x6AzPl_uVWml1Wo2bjAi37kVklYmjkbBIWe8EcyF90MMR28xcipIo2ULj5HgOizhvokPg1qDEiYetTU2MYJ1fFRVgJ5v7KF0mpUk7zBl9dvFeGT3MPfTN9EED4biqE-zRaC0r8DK7lr1PScfVHqs09sI89PXTN9Nm2lOT6c43CsUy-YhylhjJr6AfIARcFv1o0-gy0nKnWxSBYnt8NgK7rZCfpjFDUfmaYL5IXqirob98L0cWfvnV3iL6R73dNciayRDy6PqZG0sHWhKkggBIOhRqh-7VsY99qCqG76Beih-UqWPcWO_H8-4zvYDslsXMj8ff1Nk76h9pxuy-qAkRapNVAbBojf41JFVVaONqgPCH9Jszq7cpFbX6vbVq18p6hOhDb4BhuTDkT6tB30RXQWaSPqRbRs2h7VYIcuxok6wbrBzGA32hBHiSVSQs0PdheJlVy1EcHy27MJuIj_Vs6bsiq0a4GY-scs4A-68fvzsERfsLLINDrS4C9zY1f6RwfEkDsWlQ1ouBIk
> content-type: application/json
> requestid: '999993245235232323523523523535'
>
< HTTP/1.1 200 OK
< Content-Type: application/json; charset=utf-8
< Server: Microsoft-IIS/10.0
< requestid: '999993245235232323523523523535'
< CorrelationId: 3859af6f-4c8f-438c-bcfe-37ab6ecc0d6b
< RequestId: 9999af6f-4c8f-438c-bcfe-37ab6ecc0d6b
< X-Powered-By: ASP.NET
< Date: Tue, 12 Mar 2019 22:10:39 GMT
< Content-Length: 992
<
{ [992 bytes data]
This works ^
> GET /REDACTED-URL HTTP/1.1
> Host: hostname:44301
> User-Agent: curl/7.50.3
> Accept: */*
> authorization: bearer eyJ0eXA700JKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6ImF1bkM2QV9MbHFVZGNYV3Z6UzVGOURQUEFTSSIsImtpZCI6ImF1bkM2QV9MbHFVZGNYV3Z6UzVGOURQUEFTSSJ9.eyJpc3MiOiJodHRwczovL0dQLUlEUzo0NDMwMS9HUC5JRFMiLCJhdWQiOiJodHRwczovL0dQLUlEUzo0NDMwMS9HUC5JRFMvcmVzb3VyY2VzIiwiZXhwIjoxNTUyNDI5MTA1LCJuYmYiOjE1NTI0MjU1MDUsImNsaWVudF9pZCI6Ik1FUkNBRE9MSUJSRUlWUiIsImNsaWVudF9NYXJjYSI6IjEiLCJjbGllbnRfVXN1YUNvZGkiOiJEQk8iLCJjbGllbnRfVmFsaWRhRXN0YWRvQ3VlbnRhIjoiMCIsImNsaWVudF9FbnRpZGFkIjoiMzAiLCJzY29wZSI6IlByZXBhZ2FBcGkifQ.kkCGp3EJ7VkvR75Zt0MV5VRmNVf-jP5dcb7GaZF6nI5KGNUxfx_linht3nUUX4Hw55ZKhfkiUhYMrTBdx1GG97Bd99YqrbPojInugjTTnqT8TiRjqgZrZYynljfKpgfaofoBnK0EWgqsQxcxPNre2PkvxWV-x6AzPl_uVWml1Wo2bjAi37kVklYmjkbBIWe8EcyF90MMR28xcipIo2ULj5HgOizhvokPg1qDEiYetTU2MYJ1fFRVgJ5v7KF0mpUk7zBl9dvFeGT3MPfTN9EED4biqE-zRaC0r8DK7lr1PScfVHqs09sI89PXTN9Nm2lOT6c43CsUy-YhylhjJr6AfIARcFv1o0-gy0nKnWxSBYnt8NgK7rZCfpjFDUfmaYL5IXqirob98L0cWfvnV3iL6R73dNciayRDy6PqZG0sHWhKkggBIOhRqh-7VsY99qCqG76Beih-UqWPcWO_H8-4zvYDslsXMj8ff1Nk76h9pxuy-qAkRapNVAbBojf41JFVVaONqgPCH9Jszq7cpFbX6vbVq18p6hOhDb4BhuTDkT6tB30RXQWaSPqRbRs2h7VYIcuxok6wbrBzGA32hBHiSVSQs0PdheJlVy1EcHy27MJuIj_Vs6bsiq0a4GY-scs4A-68fvzsERfsLLINDrS4C9zY1f6RwfEkDsWlQ1ouBIk
> content-type: application/json
> requestid: '99999324523523534395600'
>
< HTTP/1.1 400 Bad Request
< Content-Type: text/html; charset=us-ascii
< Server: Microsoft-HTTPAPI/2.0
< Date: Tue, 12 Mar 2019 22:11:42 GMT
< Connection: close
< Content-Length: 311
<
{ [311 bytes data]
This doesn't. Same exact headers.
UPDATE: Having the same issue with wget:
With plain text:
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/10.0
requestid: 9987993245235235345225233335324324
CorrelationId: ee516440-af87-4aab-a5fa-5e563338a0be
RequestId: ee516440-af87-4aab-a5fa-5e563338a0be
X-Powered-By: ASP.NET
Date: Thu, 14 Mar 2019 21:53:16 GMT
Content-Length: 992
With variable:
HTTP/1.1 400 Bad Request
Content-Type: text/html; charset=us-ascii
Server: Microsoft-HTTPAPI/2.0
Date: Thu, 14 Mar 2019 21:54:16 GMT
Connection: close
Content-Length: 311
bash curl
I've been trying to make this work for a while now, and I just can't see why it's not working. It's just a simple script that's supposed to return either 200 (if it works) or 400 (if it doesn't). The trouble seems to come when I use variables to substitute values in Curl, example of a working curl:
curl --max-time 10 --write-out '%http_code' --silent --output '/dev/null'
--insecure -X GET https://url -H
**'authorization: bearer iUhYMrTBdx1GG97Bd99YqrbPojInugjTTnqT8TiRjqgZrZYynljfKpgfao'**
--header 'content-type: application/json' --header
"requestid: '999993122349238343493843958345'"
This works as expected and returns 200. When I replace the authorization bearer section in the following way I get a 400:
curl --max-time 10 --write-out '%http_code' --silent --output '/dev/null'
--insecure -X GET https://url -H "authorization: bearer $TOKEN" --header
'content-type: application/json' --header
"requestid: '999993122349238343493843958345'"
I tried literally everything. Using the variable as "$TOKEN", doing "'authorization: bearer $TOKEN'", even something similar to "'"'"'$TOKEN'"'"'". I don't know what else to try. When I remove the silent curl option I see the following error:
400ODY></HTML>ror 400. The request is badly formed.</p>rset=us-ascii"></HEAD>trict.dtd">
I'm lost to why it works with plain text and not variables. Any ideas?
Edit:
Added verbose for both:
> GET /REDACTED-URL HTTP/1.1
> Host: hostname:44301
> User-Agent: curl/7.50.3
> Accept: */*
> authorization: bearer eyJ0eXA700JKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6ImF1bkM2QV9MbHFVZGNYV3Z6UzVGOURQUEFTSSIsImtpZCI6ImF1bkM2QV9MbHFVZGNYV3Z6UzVGOURQUEFTSSJ9.eyJpc3MiOiJodHRwczovL0dQLUlEUzo0NDMwMS9HUC5JRFMiLCJhdWQiOiJodHRwczovL0dQLUlEUzo0NDMwMS9HUC5JRFMvcmVzb3VyY2VzIiwiZXhwIjoxNTUyNDI5MTA1LCJuYmYiOjE1NTI0MjU1MDUsImNsaWVudF9pZCI6Ik1FUkNBRE9MSUJSRUlWUiIsImNsaWVudF9NYXJjYSI6IjEiLCJjbGllbnRfVXN1YUNvZGkiOiJEQk8iLCJjbGllbnRfVmFsaWRhRXN0YWRvQ3VlbnRhIjoiMCIsImNsaWVudF9FbnRpZGFkIjoiMzAiLCJzY29wZSI6IlByZXBhZ2FBcGkifQ.kkCGp3EJ7VkvR75Zt0MV5VRmNVf-jP5dcb7GaZF6nI5KGNUxfx_linht3nUUX4Hw55ZKhfkiUhYMrTBdx1GG97Bd99YqrbPojInugjTTnqT8TiRjqgZrZYynljfKpgfaofoBnK0EWgqsQxcxPNre2PkvxWV-x6AzPl_uVWml1Wo2bjAi37kVklYmjkbBIWe8EcyF90MMR28xcipIo2ULj5HgOizhvokPg1qDEiYetTU2MYJ1fFRVgJ5v7KF0mpUk7zBl9dvFeGT3MPfTN9EED4biqE-zRaC0r8DK7lr1PScfVHqs09sI89PXTN9Nm2lOT6c43CsUy-YhylhjJr6AfIARcFv1o0-gy0nKnWxSBYnt8NgK7rZCfpjFDUfmaYL5IXqirob98L0cWfvnV3iL6R73dNciayRDy6PqZG0sHWhKkggBIOhRqh-7VsY99qCqG76Beih-UqWPcWO_H8-4zvYDslsXMj8ff1Nk76h9pxuy-qAkRapNVAbBojf41JFVVaONqgPCH9Jszq7cpFbX6vbVq18p6hOhDb4BhuTDkT6tB30RXQWaSPqRbRs2h7VYIcuxok6wbrBzGA32hBHiSVSQs0PdheJlVy1EcHy27MJuIj_Vs6bsiq0a4GY-scs4A-68fvzsERfsLLINDrS4C9zY1f6RwfEkDsWlQ1ouBIk
> content-type: application/json
> requestid: '999993245235232323523523523535'
>
< HTTP/1.1 200 OK
< Content-Type: application/json; charset=utf-8
< Server: Microsoft-IIS/10.0
< requestid: '999993245235232323523523523535'
< CorrelationId: 3859af6f-4c8f-438c-bcfe-37ab6ecc0d6b
< RequestId: 9999af6f-4c8f-438c-bcfe-37ab6ecc0d6b
< X-Powered-By: ASP.NET
< Date: Tue, 12 Mar 2019 22:10:39 GMT
< Content-Length: 992
<
{ [992 bytes data]
This works ^
> GET /REDACTED-URL HTTP/1.1
> Host: hostname:44301
> User-Agent: curl/7.50.3
> Accept: */*
> authorization: bearer eyJ0eXA700JKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6ImF1bkM2QV9MbHFVZGNYV3Z6UzVGOURQUEFTSSIsImtpZCI6ImF1bkM2QV9MbHFVZGNYV3Z6UzVGOURQUEFTSSJ9.eyJpc3MiOiJodHRwczovL0dQLUlEUzo0NDMwMS9HUC5JRFMiLCJhdWQiOiJodHRwczovL0dQLUlEUzo0NDMwMS9HUC5JRFMvcmVzb3VyY2VzIiwiZXhwIjoxNTUyNDI5MTA1LCJuYmYiOjE1NTI0MjU1MDUsImNsaWVudF9pZCI6Ik1FUkNBRE9MSUJSRUlWUiIsImNsaWVudF9NYXJjYSI6IjEiLCJjbGllbnRfVXN1YUNvZGkiOiJEQk8iLCJjbGllbnRfVmFsaWRhRXN0YWRvQ3VlbnRhIjoiMCIsImNsaWVudF9FbnRpZGFkIjoiMzAiLCJzY29wZSI6IlByZXBhZ2FBcGkifQ.kkCGp3EJ7VkvR75Zt0MV5VRmNVf-jP5dcb7GaZF6nI5KGNUxfx_linht3nUUX4Hw55ZKhfkiUhYMrTBdx1GG97Bd99YqrbPojInugjTTnqT8TiRjqgZrZYynljfKpgfaofoBnK0EWgqsQxcxPNre2PkvxWV-x6AzPl_uVWml1Wo2bjAi37kVklYmjkbBIWe8EcyF90MMR28xcipIo2ULj5HgOizhvokPg1qDEiYetTU2MYJ1fFRVgJ5v7KF0mpUk7zBl9dvFeGT3MPfTN9EED4biqE-zRaC0r8DK7lr1PScfVHqs09sI89PXTN9Nm2lOT6c43CsUy-YhylhjJr6AfIARcFv1o0-gy0nKnWxSBYnt8NgK7rZCfpjFDUfmaYL5IXqirob98L0cWfvnV3iL6R73dNciayRDy6PqZG0sHWhKkggBIOhRqh-7VsY99qCqG76Beih-UqWPcWO_H8-4zvYDslsXMj8ff1Nk76h9pxuy-qAkRapNVAbBojf41JFVVaONqgPCH9Jszq7cpFbX6vbVq18p6hOhDb4BhuTDkT6tB30RXQWaSPqRbRs2h7VYIcuxok6wbrBzGA32hBHiSVSQs0PdheJlVy1EcHy27MJuIj_Vs6bsiq0a4GY-scs4A-68fvzsERfsLLINDrS4C9zY1f6RwfEkDsWlQ1ouBIk
> content-type: application/json
> requestid: '99999324523523534395600'
>
< HTTP/1.1 400 Bad Request
< Content-Type: text/html; charset=us-ascii
< Server: Microsoft-HTTPAPI/2.0
< Date: Tue, 12 Mar 2019 22:11:42 GMT
< Connection: close
< Content-Length: 311
<
{ [311 bytes data]
This doesn't. Same exact headers.
UPDATE: Having the same issue with wget:
With plain text:
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/10.0
requestid: 9987993245235235345225233335324324
CorrelationId: ee516440-af87-4aab-a5fa-5e563338a0be
RequestId: ee516440-af87-4aab-a5fa-5e563338a0be
X-Powered-By: ASP.NET
Date: Thu, 14 Mar 2019 21:53:16 GMT
Content-Length: 992
With variable:
HTTP/1.1 400 Bad Request
Content-Type: text/html; charset=us-ascii
Server: Microsoft-HTTPAPI/2.0
Date: Thu, 14 Mar 2019 21:54:16 GMT
Connection: close
Content-Length: 311
bash curl
bash curl
edited Mar 14 at 21:59
Rodrigo
asked Mar 12 at 21:26
RodrigoRodrigo
42
42
How did you setTOKEN? If you useset -xand then run thecurlcommand, what does the shell show as the argument to-H?
– ilkkachu
Mar 12 at 21:35
$TOKEN was set just plainly on top of the script as: TOKEN="iUhYMrTBdx1GG97Bd99YqrbPojInugjTTnqT8TiRjqgZrZYynljfKpgfao"
– Rodrigo
Mar 12 at 22:03
well yeah, it looks like it should work, so that's why I asked about whatset -xshows. You are sure that you used double quotes around the"authorization: bearer $TOKEN"as you have posted here, and not single quotes, right?
– ilkkachu
Mar 12 at 22:12
Absolutely sure. I tried all combinations anyway, I updated the post showing that the headers are being passed identically as text, yet when I use the variable it fails.
– Rodrigo
Mar 12 at 22:27
Ok. What about invisible characters like non-breaking spaces or CRLF line endings? Ifcurldoesn't show them specially in the output, you wouldn't see them, but the variable assignment might have one in the end. Though with CRLF, you'd probably have the whole file with those line endings anyway, and then the version that doesn't use a variable wouldn't work either.
– ilkkachu
Mar 13 at 11:36
add a comment |
How did you setTOKEN? If you useset -xand then run thecurlcommand, what does the shell show as the argument to-H?
– ilkkachu
Mar 12 at 21:35
$TOKEN was set just plainly on top of the script as: TOKEN="iUhYMrTBdx1GG97Bd99YqrbPojInugjTTnqT8TiRjqgZrZYynljfKpgfao"
– Rodrigo
Mar 12 at 22:03
well yeah, it looks like it should work, so that's why I asked about whatset -xshows. You are sure that you used double quotes around the"authorization: bearer $TOKEN"as you have posted here, and not single quotes, right?
– ilkkachu
Mar 12 at 22:12
Absolutely sure. I tried all combinations anyway, I updated the post showing that the headers are being passed identically as text, yet when I use the variable it fails.
– Rodrigo
Mar 12 at 22:27
Ok. What about invisible characters like non-breaking spaces or CRLF line endings? Ifcurldoesn't show them specially in the output, you wouldn't see them, but the variable assignment might have one in the end. Though with CRLF, you'd probably have the whole file with those line endings anyway, and then the version that doesn't use a variable wouldn't work either.
– ilkkachu
Mar 13 at 11:36
How did you set
TOKEN? If you use set -x and then run the curl command, what does the shell show as the argument to -H?– ilkkachu
Mar 12 at 21:35
How did you set
TOKEN? If you use set -x and then run the curl command, what does the shell show as the argument to -H?– ilkkachu
Mar 12 at 21:35
$TOKEN was set just plainly on top of the script as: TOKEN="iUhYMrTBdx1GG97Bd99YqrbPojInugjTTnqT8TiRjqgZrZYynljfKpgfao"
– Rodrigo
Mar 12 at 22:03
$TOKEN was set just plainly on top of the script as: TOKEN="iUhYMrTBdx1GG97Bd99YqrbPojInugjTTnqT8TiRjqgZrZYynljfKpgfao"
– Rodrigo
Mar 12 at 22:03
well yeah, it looks like it should work, so that's why I asked about what
set -x shows. You are sure that you used double quotes around the "authorization: bearer $TOKEN" as you have posted here, and not single quotes, right?– ilkkachu
Mar 12 at 22:12
well yeah, it looks like it should work, so that's why I asked about what
set -x shows. You are sure that you used double quotes around the "authorization: bearer $TOKEN" as you have posted here, and not single quotes, right?– ilkkachu
Mar 12 at 22:12
Absolutely sure. I tried all combinations anyway, I updated the post showing that the headers are being passed identically as text, yet when I use the variable it fails.
– Rodrigo
Mar 12 at 22:27
Absolutely sure. I tried all combinations anyway, I updated the post showing that the headers are being passed identically as text, yet when I use the variable it fails.
– Rodrigo
Mar 12 at 22:27
Ok. What about invisible characters like non-breaking spaces or CRLF line endings? If
curl doesn't show them specially in the output, you wouldn't see them, but the variable assignment might have one in the end. Though with CRLF, you'd probably have the whole file with those line endings anyway, and then the version that doesn't use a variable wouldn't work either.– ilkkachu
Mar 13 at 11:36
Ok. What about invisible characters like non-breaking spaces or CRLF line endings? If
curl doesn't show them specially in the output, you wouldn't see them, but the variable assignment might have one in the end. Though with CRLF, you'd probably have the whole file with those line endings anyway, and then the version that doesn't use a variable wouldn't work either.– ilkkachu
Mar 13 at 11:36
add a comment |
1 Answer
1
active
oldest
votes
Assuming you set TOKEN as environment variable with export TOKEN=foo you should use $TOKEN in your header string.
curl --max-time 10 --write-out '%http_code' --silent --output '/dev/null'
--insecure -X GET https://url -H "authorization: bearer $TOKEN" --header
'content-type: application/json' --header
"requestid: '999993122349238343493843958345'"
If you are setting the TOKEN in a script with TOKEN=foo you should use $TOKEN in your header string:
curl --max-time 10 --write-out '%http_code' --verbose --output '/dev/null'
--insecure -X GET https://sp-codes.de -H "authorization: bearer $TOKEN" --header
'content-type: application/json' --header
"requestid: '999993122349238343493843958345'"
As alternative -H "authorization: bearer "$TOKEN should work in both cases.
Using the --verbose option instead of --silent will print the following header information in all cases explained above (excerpt):
> GET / HTTP/2
> Host: url
> User-Agent: curl/7.58.0
> Accept: */*
> authorization: bearer foo
> content-type: application/json
> requestid: '999993122349238343493843958345'
It is also very important to use double quotes, because single quotes will not be parsed.
With --verbose on I can see both with $TOKEN and the plain text (working one) are giving the exact same authorization: bearer text. Yet one works and the other doesn't.
– Rodrigo
Mar 12 at 22:16
Have you tried-H "authorization: bearer $TOKEN"?
– Samuel Philipp
Mar 12 at 22:19
Yes. Same issue. Tried all possible combinations of variable display. Not sure if this is some kind of bug with Curl itself.
– Rodrigo
Mar 12 at 22:21
Which version of curl are you using (curl --version)?
– Samuel Philipp
Mar 12 at 22:22
curl 7.50.3 (i686-pc-cygwin)
– Rodrigo
Mar 12 at 22:23
|
show 9 more comments
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f505966%2fpassing-variable-to-authorization-bearer-header-with-curl-and-bash%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Assuming you set TOKEN as environment variable with export TOKEN=foo you should use $TOKEN in your header string.
curl --max-time 10 --write-out '%http_code' --silent --output '/dev/null'
--insecure -X GET https://url -H "authorization: bearer $TOKEN" --header
'content-type: application/json' --header
"requestid: '999993122349238343493843958345'"
If you are setting the TOKEN in a script with TOKEN=foo you should use $TOKEN in your header string:
curl --max-time 10 --write-out '%http_code' --verbose --output '/dev/null'
--insecure -X GET https://sp-codes.de -H "authorization: bearer $TOKEN" --header
'content-type: application/json' --header
"requestid: '999993122349238343493843958345'"
As alternative -H "authorization: bearer "$TOKEN should work in both cases.
Using the --verbose option instead of --silent will print the following header information in all cases explained above (excerpt):
> GET / HTTP/2
> Host: url
> User-Agent: curl/7.58.0
> Accept: */*
> authorization: bearer foo
> content-type: application/json
> requestid: '999993122349238343493843958345'
It is also very important to use double quotes, because single quotes will not be parsed.
With --verbose on I can see both with $TOKEN and the plain text (working one) are giving the exact same authorization: bearer text. Yet one works and the other doesn't.
– Rodrigo
Mar 12 at 22:16
Have you tried-H "authorization: bearer $TOKEN"?
– Samuel Philipp
Mar 12 at 22:19
Yes. Same issue. Tried all possible combinations of variable display. Not sure if this is some kind of bug with Curl itself.
– Rodrigo
Mar 12 at 22:21
Which version of curl are you using (curl --version)?
– Samuel Philipp
Mar 12 at 22:22
curl 7.50.3 (i686-pc-cygwin)
– Rodrigo
Mar 12 at 22:23
|
show 9 more comments
Assuming you set TOKEN as environment variable with export TOKEN=foo you should use $TOKEN in your header string.
curl --max-time 10 --write-out '%http_code' --silent --output '/dev/null'
--insecure -X GET https://url -H "authorization: bearer $TOKEN" --header
'content-type: application/json' --header
"requestid: '999993122349238343493843958345'"
If you are setting the TOKEN in a script with TOKEN=foo you should use $TOKEN in your header string:
curl --max-time 10 --write-out '%http_code' --verbose --output '/dev/null'
--insecure -X GET https://sp-codes.de -H "authorization: bearer $TOKEN" --header
'content-type: application/json' --header
"requestid: '999993122349238343493843958345'"
As alternative -H "authorization: bearer "$TOKEN should work in both cases.
Using the --verbose option instead of --silent will print the following header information in all cases explained above (excerpt):
> GET / HTTP/2
> Host: url
> User-Agent: curl/7.58.0
> Accept: */*
> authorization: bearer foo
> content-type: application/json
> requestid: '999993122349238343493843958345'
It is also very important to use double quotes, because single quotes will not be parsed.
With --verbose on I can see both with $TOKEN and the plain text (working one) are giving the exact same authorization: bearer text. Yet one works and the other doesn't.
– Rodrigo
Mar 12 at 22:16
Have you tried-H "authorization: bearer $TOKEN"?
– Samuel Philipp
Mar 12 at 22:19
Yes. Same issue. Tried all possible combinations of variable display. Not sure if this is some kind of bug with Curl itself.
– Rodrigo
Mar 12 at 22:21
Which version of curl are you using (curl --version)?
– Samuel Philipp
Mar 12 at 22:22
curl 7.50.3 (i686-pc-cygwin)
– Rodrigo
Mar 12 at 22:23
|
show 9 more comments
Assuming you set TOKEN as environment variable with export TOKEN=foo you should use $TOKEN in your header string.
curl --max-time 10 --write-out '%http_code' --silent --output '/dev/null'
--insecure -X GET https://url -H "authorization: bearer $TOKEN" --header
'content-type: application/json' --header
"requestid: '999993122349238343493843958345'"
If you are setting the TOKEN in a script with TOKEN=foo you should use $TOKEN in your header string:
curl --max-time 10 --write-out '%http_code' --verbose --output '/dev/null'
--insecure -X GET https://sp-codes.de -H "authorization: bearer $TOKEN" --header
'content-type: application/json' --header
"requestid: '999993122349238343493843958345'"
As alternative -H "authorization: bearer "$TOKEN should work in both cases.
Using the --verbose option instead of --silent will print the following header information in all cases explained above (excerpt):
> GET / HTTP/2
> Host: url
> User-Agent: curl/7.58.0
> Accept: */*
> authorization: bearer foo
> content-type: application/json
> requestid: '999993122349238343493843958345'
It is also very important to use double quotes, because single quotes will not be parsed.
Assuming you set TOKEN as environment variable with export TOKEN=foo you should use $TOKEN in your header string.
curl --max-time 10 --write-out '%http_code' --silent --output '/dev/null'
--insecure -X GET https://url -H "authorization: bearer $TOKEN" --header
'content-type: application/json' --header
"requestid: '999993122349238343493843958345'"
If you are setting the TOKEN in a script with TOKEN=foo you should use $TOKEN in your header string:
curl --max-time 10 --write-out '%http_code' --verbose --output '/dev/null'
--insecure -X GET https://sp-codes.de -H "authorization: bearer $TOKEN" --header
'content-type: application/json' --header
"requestid: '999993122349238343493843958345'"
As alternative -H "authorization: bearer "$TOKEN should work in both cases.
Using the --verbose option instead of --silent will print the following header information in all cases explained above (excerpt):
> GET / HTTP/2
> Host: url
> User-Agent: curl/7.58.0
> Accept: */*
> authorization: bearer foo
> content-type: application/json
> requestid: '999993122349238343493843958345'
It is also very important to use double quotes, because single quotes will not be parsed.
edited Mar 13 at 6:45
answered Mar 12 at 21:41
Samuel PhilippSamuel Philipp
269211
269211
With --verbose on I can see both with $TOKEN and the plain text (working one) are giving the exact same authorization: bearer text. Yet one works and the other doesn't.
– Rodrigo
Mar 12 at 22:16
Have you tried-H "authorization: bearer $TOKEN"?
– Samuel Philipp
Mar 12 at 22:19
Yes. Same issue. Tried all possible combinations of variable display. Not sure if this is some kind of bug with Curl itself.
– Rodrigo
Mar 12 at 22:21
Which version of curl are you using (curl --version)?
– Samuel Philipp
Mar 12 at 22:22
curl 7.50.3 (i686-pc-cygwin)
– Rodrigo
Mar 12 at 22:23
|
show 9 more comments
With --verbose on I can see both with $TOKEN and the plain text (working one) are giving the exact same authorization: bearer text. Yet one works and the other doesn't.
– Rodrigo
Mar 12 at 22:16
Have you tried-H "authorization: bearer $TOKEN"?
– Samuel Philipp
Mar 12 at 22:19
Yes. Same issue. Tried all possible combinations of variable display. Not sure if this is some kind of bug with Curl itself.
– Rodrigo
Mar 12 at 22:21
Which version of curl are you using (curl --version)?
– Samuel Philipp
Mar 12 at 22:22
curl 7.50.3 (i686-pc-cygwin)
– Rodrigo
Mar 12 at 22:23
With --verbose on I can see both with $TOKEN and the plain text (working one) are giving the exact same authorization: bearer text. Yet one works and the other doesn't.
– Rodrigo
Mar 12 at 22:16
With --verbose on I can see both with $TOKEN and the plain text (working one) are giving the exact same authorization: bearer text. Yet one works and the other doesn't.
– Rodrigo
Mar 12 at 22:16
Have you tried
-H "authorization: bearer $TOKEN"?– Samuel Philipp
Mar 12 at 22:19
Have you tried
-H "authorization: bearer $TOKEN"?– Samuel Philipp
Mar 12 at 22:19
Yes. Same issue. Tried all possible combinations of variable display. Not sure if this is some kind of bug with Curl itself.
– Rodrigo
Mar 12 at 22:21
Yes. Same issue. Tried all possible combinations of variable display. Not sure if this is some kind of bug with Curl itself.
– Rodrigo
Mar 12 at 22:21
Which version of curl are you using (
curl --version)?– Samuel Philipp
Mar 12 at 22:22
Which version of curl are you using (
curl --version)?– Samuel Philipp
Mar 12 at 22:22
curl 7.50.3 (i686-pc-cygwin)
– Rodrigo
Mar 12 at 22:23
curl 7.50.3 (i686-pc-cygwin)
– Rodrigo
Mar 12 at 22:23
|
show 9 more comments
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f505966%2fpassing-variable-to-authorization-bearer-header-with-curl-and-bash%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
-bash, curl
How did you set
TOKEN? If you useset -xand then run thecurlcommand, what does the shell show as the argument to-H?– ilkkachu
Mar 12 at 21:35
$TOKEN was set just plainly on top of the script as: TOKEN="iUhYMrTBdx1GG97Bd99YqrbPojInugjTTnqT8TiRjqgZrZYynljfKpgfao"
– Rodrigo
Mar 12 at 22:03
well yeah, it looks like it should work, so that's why I asked about what
set -xshows. You are sure that you used double quotes around the"authorization: bearer $TOKEN"as you have posted here, and not single quotes, right?– ilkkachu
Mar 12 at 22:12
Absolutely sure. I tried all combinations anyway, I updated the post showing that the headers are being passed identically as text, yet when I use the variable it fails.
– Rodrigo
Mar 12 at 22:27
Ok. What about invisible characters like non-breaking spaces or CRLF line endings? If
curldoesn't show them specially in the output, you wouldn't see them, but the variable assignment might have one in the end. Though with CRLF, you'd probably have the whole file with those line endings anyway, and then the version that doesn't use a variable wouldn't work either.– ilkkachu
Mar 13 at 11:36