3

I am trying to pass a Bearer token (variable $token)to invoke a job via curl command. However the single quote after the -H is not letting the value of variable $token being passed to curl command.

curl -X POST 'https://server.domain.com/v2/jobs/28723316-9373-44ba-9229-7c796f21b099/runs?project_id=aff59748-260a-476e-9578-b4f4a93e7a92' -H 'Content-Type: application/json' -H 'Authorization: Bearer $token  -d { "job_run": {} }'

I get this error:

{"code":400,"error":"Bad Request","reason":"Bearer token format is invalid. Expected: 'Bearer '. Received: 'Bearer $token -d { "job_run": {} }'.","message":"Bearer token is invalid."}

I tried adding like the escape character with the variable $token:

curl -X POST 'https://server.domain.com/v2/jobs/28723316-9373-44ba-9229-7c796f21b099/runs?project_id=aff59748-260a-476e-9578-b4f4a93e7a92' -H 'Content-Type: application/json' -H 'Authorization: Bearer "\$token\"  -d { "job_run": {} }'

I get the same error: {"code":400,"error":"Bad Request","reason":"Bearer token format is invalid. Expected: 'Bearer '. Received: 'Bearer "\$token\" -d { "job_run": {} }'.","message":"Bearer token is invalid."}

I tried double quotes as well, it has been a few hours and I am unable to extract he variable value $token within single quotes.

Could some please assist and give me the correct syntax?

Thanks in advance

user3605317
  • 127
  • 2
  • 9
  • 1
    https://stackoverflow.com/questions/13799789/expansion-of-variables-inside-single-quotes-in-a-command-in-bash – Palash Goel Feb 11 '21 at 07:44
  • try curl -X POST 'https://server.domain.com/v2/jobs/28723316-9373-44ba-9229-7c796f21b099/runs?project_id=aff59748-260a-476e-9578-b4f4a93e7a92' -H 'Content-Type: application/json' -H 'Authorization: Bearer ' $token -d '{ "job_run": {} }' – Ricco D Feb 11 '21 at 07:45
  • 1
    @RiccoD: That probably won't work, `curl` expects the entire header to be in the same argument, in your example you have `'Authorization: Bearer '` and `$token` as two distinct arguments. – user000001 Feb 11 '21 at 09:49

1 Answers1

8

The problem here are the quotes. It should be like this:

curl -X POST 'https://server.domain.com/v2/jobs/28723316-9373-44ba-9229-7c796f21b099/runs?project_id=aff59748-260a-476e-9578-b4f4a93e7a92' -H 'Content-Type: application/json' -H "Authorization: Bearer $token"  -d '{ "job_run": {} }'

In multiline:

curl -X POST 'https://server.domain.com/v2/jobs/28723316-9373-44ba-9229-7c796f21b099/runs?project_id=aff59748-260a-476e-9578-b4f4a93e7a92' \
     -H 'Content-Type: application/json' \
     -H "Authorization: Bearer $token"  \ 
     -d '{ "job_run": {} }'

Specifically, variables in bash aren't interpolated when in single quotes ('). Thus, we set the dynamic string inside double quotes (")

-H "Authorization: Bearer $token"

Also the -H and -d arguments are distinct, they should be quoted separately, in your code you have them combined in a single argument.

user000001
  • 30,389
  • 12
  • 73
  • 103
  • 2
    You are a genius, this worked a charm, I tried so many things for hours. That was the problem where the variable wasn't get populating within single quotes. Thank you so much!!!. – user3605317 Feb 12 '21 at 02:49