What You'll Learn
- How HTTP Basic Authentication works
- How Bearer token authentication works
- How to send each in cURL and Postman
- Why Basic Auth is mostly obsolete for APIs
Why This Matters
The Authorization header is the standard way to send credentials with HTTP requests. Two schemes dominate: Basic (username/password encoded as Base64) and Bearer (an opaque token). Knowing how each works on the wire helps you debug auth failures and choose the right scheme for your API.
The Authorization Header
Both schemes use the same header:
Authorization: <scheme> <credentials>
The scheme is the first word (Basic, Bearer, Digest, etc.). The credentials follow, separated by a space.
Part 1 — Basic Authentication
Basic Auth sends a username and password with every request. The credentials are combined as username:password, encoded with Base64, and sent in the Authorization header.
How the credential is built
username = "anita"
password = "secret123"
combined = "anita:secret123"
base64 = "YW5pdGE6c2VjcmV0MTIz"
The request
GET /users/me HTTP/1.1
Authorization: Basic YW5pdGE6c2VjcmV0MTIz
Important: Base64 is not encryption
Base64 is just an encoding — anyone can decode it instantly. Basic Auth must always be used over HTTPS. Over plain HTTP, the credentials are effectively sent in clear text.
YW5pdGE6c2VjcmV0MTIz into any Base64 decoder and you'll get anita:secret123. Basic Auth provides zero confidentiality — only HTTPS does.
Sending Basic Auth in cURL
curl -u anita:secret123 https://api.example.com/users/me
The -u flag tells cURL to use Basic Auth. cURL handles the Base64 encoding for you.
Sending Basic Auth in Postman
- Click the Authorization tab.
- Type: Basic Auth.
- Username:
anita. - Password:
secret123.
When to use Basic Auth
- Internal tools behind a VPN or firewall.
- Quick prototyping where you need just one credential.
- Legacy APIs that still require it.
For new APIs, prefer Bearer tokens (next).
Why Basic Auth is mostly obsolete for APIs
- Credentials are sent every request. If any request is intercepted, credentials leak.
- No expiration. The username/password is valid forever until you change it.
- No scopes. The credential grants full access — no way to limit it.
- Cannot be revoked without changing the password. Which logs out every other client too.
- Identifies a user, not a client. Two apps using the same account share the same credential.
Part 2 — Bearer Tokens
Bearer token authentication sends an opaque string (the token) in the Authorization header. The token represents a previously-authenticated session. The server does not care what the token is — it just checks if it's valid.
The request
GET /users/me HTTP/1.1
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjMifQ.abc123
"Bearer" means "the holder of this token"
The word "Bearer" literally means "whoever holds this token is granted access". This is why tokens must be kept secret — anyone who has the token can use it. If a token leaks, the holder has full access until the token expires or is revoked.
How tokens are obtained
Bearer tokens are not permanent credentials. They are obtained through a flow like:
- User logs in with username/password.
- Server verifies credentials and issues a token.
- Client stores the token and sends it with every subsequent request.
- Token expires (often after 1 hour). Client must re-authenticate to get a new one.
Sending Bearer tokens in cURL
curl -H "Authorization: Bearer YOUR_TOKEN" \
https://api.example.com/users/me
Sending Bearer tokens in Postman
- Click the Authorization tab.
- Type: Bearer Token.
- Token: paste your token.
Bearer Tokens vs API Keys
Both are sent as Authorization: Bearer <value>. The difference is lifecycle and scope:
| Aspect | API Key | Bearer Token |
|---|---|---|
| Lifetime | Long (months) | Short (hours) |
| Identifies | The client/app | A specific user/session |
| Obtained via | Dashboard, generated by developer | Login flow, OAuth, etc. |
| Scopes | Usually all-or-nothing | Often fine-grained |
| Expiration | Manual | Automatic (exp claim) |
The Login → Token → Use Flow
# Step 1: Login
POST /login HTTP/1.1
Content-Type: application/json
{"username": "anita", "password": "secret123"}
# Response
HTTP/1.1 200 OK
{
"access_token": "eyJhbGciOi...",
"expires_in": 3600
}
# Step 2: Use the token
GET /users/me HTTP/1.1
Authorization: Bearer eyJhbGciOi...
This is the standard "login then use token" flow. We cover variations (refresh tokens, OAuth) in later lessons.
Common Mistakes
- Using Basic Auth over HTTP. Credentials are sent in clear text. Always use HTTPS.
- Hard-coding tokens in frontend code. Tokens leak from browser dev tools. Use a backend proxy.
- Forgetting the "Bearer " prefix.
Authorization: eyJ...is invalid. Must beAuthorization: Bearer eyJ...(with the space). - Not handling token expiration. Tokens expire. Your client must detect 401 and refresh.
- Storing tokens in localStorage for sensitive apps. localStorage is vulnerable to XSS. Use HttpOnly cookies for high-security apps.
- Sending the wrong scheme.
Authorization: Basic <token>when the API expectsBearer. The server returns 401.
Practical Exercise (10 minutes)
- Decode this Base64 string:
YW5pdGE6c2VjcmV0MTIz— confirm it'sanita:secret123. - Use cURL with
-uto call any API that uses Basic Auth (e.g., a test HTTPBin endpoint:curl -u user:pass https://httpbin.org/basic-auth/user/pass). - Use cURL with
-H "Authorization: Bearer ..."to call any API that uses Bearer tokens (e.g., GitHub:curl -H "Authorization: Bearer YOUR_GITHUB_TOKEN" https://api.github.com/user). - Try sending a Bearer token without the "Bearer " prefix. Observe the 401 error.
Mini Challenge
Implement a simple login flow in Postman: (1) POST to a login endpoint with username/password, (2) extract the token from the response using a test script, (3) save it as an environment variable, (4) use it in a second request as a Bearer token. (Hint: Postman's Tests tab can run pm.environment.set("token", responseBody.token).)
Key Takeaways
- Both Basic and Bearer use the
Authorizationheader:<scheme> <credentials>. - Basic Auth = Base64-encoded
username:password. Always use HTTPS; Base64 is not encryption. - Bearer = an opaque token obtained from a login flow or OAuth.
- Basic Auth is mostly obsolete for APIs — it sends credentials every request and has no expiration or scopes.
- Bearer tokens expire and can be scoped. Always include the "Bearer " prefix.
Previously: Lesson 27 covered API keys.
Today: You learned Basic Auth and Bearer tokens — the two most common HTTP auth schemes.
Next: Lesson 29 dives into JWT (JSON Web Tokens) — the most popular Bearer token format.
FAQ
What does "Bearer" actually mean?
It means "whoever bears (holds) this token is granted access". The token itself is the proof — there is no other authentication. This is why keeping tokens secret is critical: anyone who has the token has full access.
Is Bearer token the same as JWT?
Not exactly. Bearer is the scheme (how the token is sent). JWT is a token format (what the token contains). Most Bearer tokens today are JWTs, but they could be any opaque string the server recognizes.
Comments
Comments
Post a Comment