Lukas Garcia
Posted on February 6, 2021
About Basic Auth
In Basic Authentication, a HTTP request contains a header Authorization: Basic <credentials>
, where credentials is the Base64 encoding of username and password joined by a single colon :
.
Basic Auth is considered as not safe enough, but we still use it a lot for some less sensitive stuff because it is easy to set up.
Basic Auth with curl
You can use Basic Http Auth with curl in following two ways:
Option 1: Pass credentials to curl
Passing Basic credentials to curl command is easy as this:
curl -u username:password https://example.com
If your username or password contains a special character, such as white-space, then you might want to surround credentials with single quotes:
curl -u 'username:password' https://example.com
Option 2: Pass Authorization header
If you want to have a full control over your HTTP request, you might want to Base64 encode your username:password
and place it into Authorization
header.
Curl command should look like this:
curl -H 'Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=' https://example.com
Basic Auth and colon in username
Colon :
is not allowed to be used in an username according to Basic Auth specification. But in case you need to support it (ie your users are allowed to create such username) and you are in charge of client and server, you might want to URL encode the username and password on both sides.
Posted on February 6, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.