The HTTP Authorization request header is sometimes required to authenticate a user agent with a server.
This post explains how to create the header on linux at command line.
Syntax
The HTTP Authorization request header has the following syntax:
The type is typically “Basic”, in which case the credentials are of the form user:password
encoded as base64.
Curl will generate this header for us if we use the -u
option:
Now for the real question, how do we generate this header for use with curl’s -H
option?
Experiments
Here is the first attempt to base64 encode user:password
that is WRONG:
Notice that dXNlcjpwYXNzd29yZAo=
does not equal dXNlcjpwYXNzd29yZA==
.
Let’s decode to find the difference:
That %
is different. What is that?
The %
symbol is how zsh handles the end of partial lines.
In other words, zsh assumes we want our prompt on a newline even if the last command didn’t end with
a newline. That %
symbol is how we know the difference between a newline the last command output
and a newline zsh graciously inserted for us.
Let’s execute this command under bash and see the difference:
|
|
The base64 encoded user:password
that curl generated is not terminated with a newline, unlike the one we generated.
Let’s see if we can get echo
to not add that newline.
|
|
The -n
option for echo
is what we want.
We see that the output is exactly the same as what curl generated.
Putting it all together:
|
|
Take Away
In zsh, a %
symbol is placed at the end of a line where zsh inserted a newline for us.
It is important to include echo’s -n
option when piping content to an encoder to avoid a
newline character being included in the encoded output.
|
|
Experimental Setup
|
|