0

I have a bash script with the following curl command :

curl -v -X GET https://example.com -H ~/Desktop/headers.txt -o ~/Desktop/file.txt

When i run the bash script i am getting unothorized 401. Obviously i am not reading the header file correctly. My header file looks like that "Authorization: Bearer 40bsafsdgds34234....". My curl version is 7.26

2
  • not sure if this might be the problem, but you should put the options before the URL: curl [options] [URL...].
    – pLumo
    Sep 28, 2018 at 14:08
  • That's not the problem sadly. Thanks for the suggestion.
    – Thanos M
    Sep 28, 2018 at 14:10

2 Answers 2

5

-H option reads header information per default from the argument (text), not from files.

Since curl 7.55 you can use -H @file to read from a file.


For old versions you need a small script, something like this:

curl $(xargs -a headers.txt -I{} printf '-H "%s" ' "{}") URL

Some Links:


Alternative:

Create the config file including -H "...":

-H "Authorization: bearer token"
-H "Another header"

and use -K/--config <config file> option.

or automatically with your original file via sed:

curl -K <(sed -r 's/^(.*)$/-H "\1"/' headers.txt) URL
1

After a lot of trial and error i found out that this works for me

curl -v -X GET https://example.com -H "$(cat ~/Desktop/headers.txt)" -o ~/Desktop/file.txt 
0

You must log in to answer this question.

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