1

I writing a bash script to do a curl with custom headers. The request looks like

curl --location --request GET 'http://100.200.159.79:80/n_clone/' \
--header 'Authorization: Bearer 5e0333f03d311b646f84424a047abef08433d9ed' \
--header 'Content-Type: multipart/form-data; boundary=--------------------------905029954124550761104092'

I want to add a variable after Bearer like

access_token= "5e0af6333f03b646f84424a047abef08433d9ed77411b4f4"
auth_head= "'Authorization: Bearer'$access_to"

get_data=$(curl --location --request GET "http://100.200.159.79:80/n_clone/" \
-H "Authorization: Bearer $access_token" \
-H "Content-Type: multipart/form-data; boundary=--------------------------651618584466976849992097")

I have also tried

get_data=$(curl --location --request GET 'http://100.200.159.79:80/n_clone/' \
-H 'Authorization: Bearer '$access_token \
-H 'Content-Type: multipart/form-data; boundary=--------------------------651618584466976849992097')

How can I insert a variable between '' (single quotes) such that the curl request works fine? At this time it throws an error of Authorization field missing.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Rahul
  • 895
  • 1
  • 13
  • 26
  • 1
    You shouldn't have *any* literal quotes in the header at all. Don't try to inject them. The only quotes that should be here are *syntactic* ones, read and then removed by the shell itself as it figures out how your string is supposed to be broken down into a list of individual arguments. – Charles Duffy Dec 31 '19 at 15:26
  • Also, spaces around the `=` in an assignment are disallowed. – Charles Duffy Dec 31 '19 at 15:27

1 Answers1

2

Just use double quotes for the whole string; there's no need for single quotes anywhere in the authorization header's definition at all.

access_token="5e0af6333f03b646f84424a047abef08433d9ed77411b4f4"
get_data=$(curl --location --request GET 'http://100.200.159.79:80/n_clone/' \
-H "Authorization: Bearer $access_token" \
-H 'Content-Type: multipart/form-data; boundary=--------------------------651618584466976849992097')

The only quotes we want here are syntactic ones, used by the shell. When you put quotes inside other quotes, they become literal data, passed to curl. If the remote server doesn't expect literal quote characters in its headers, we don't ever want to pass literal quotes to curl.


By the way, it would be equally valid to use:

-H 'Authorization: Bearer '"$access_token" \

...wherein we have the string Authorization: Bearer in a single-quoted context and then expand $access_token in a double-quoted context; the reason I didn't suggest that above is that Authorization: Bearer expands the same way in single and double quotes, so why use two different contexts when you could have only one?

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • 1
    BTW, this has been answered community-wiki as I believe the question is a duplicate, but haven't found the preexisting instance to flag it yet; don't want to be getting reputation points for knowingly answering a dupe. – Charles Duffy Dec 31 '19 at 15:30
  • thanks got the hang of it. Marking the question as duplicate. – Rahul Dec 31 '19 at 15:54