Keyboard Shortcuts N Next post
P Previous post
S Save / unsave
R Read aloud
T Toggle theme
/ Focus search
Esc Close panels
🔥
Ready to read...
APIs APIs From Zero to Real-World API Testing & Automation Authentication HTTP Module 5 — API Authentication

Basic Authentication and Bearer Tokens — How They Work

Reviewed & accurate
AI Summary

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.

Base64 is reversible. Paste 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

  1. Click the Authorization tab.
  2. Type: Basic Auth.
  3. Username: anita.
  4. 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

  1. Credentials are sent every request. If any request is intercepted, credentials leak.
  2. No expiration. The username/password is valid forever until you change it.
  3. No scopes. The credential grants full access — no way to limit it.
  4. Cannot be revoked without changing the password. Which logs out every other client too.
  5. 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:

  1. User logs in with username/password.
  2. Server verifies credentials and issues a token.
  3. Client stores the token and sends it with every subsequent request.
  4. 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

  1. Click the Authorization tab.
  2. Type: Bearer Token.
  3. Token: paste your token.

Bearer Tokens vs API Keys

Both are sent as Authorization: Bearer <value>. The difference is lifecycle and scope:

AspectAPI KeyBearer Token
LifetimeLong (months)Short (hours)
IdentifiesThe client/appA specific user/session
Obtained viaDashboard, generated by developerLogin flow, OAuth, etc.
ScopesUsually all-or-nothingOften fine-grained
ExpirationManualAutomatic (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

  1. Using Basic Auth over HTTP. Credentials are sent in clear text. Always use HTTPS.
  2. Hard-coding tokens in frontend code. Tokens leak from browser dev tools. Use a backend proxy.
  3. Forgetting the "Bearer " prefix. Authorization: eyJ... is invalid. Must be Authorization: Bearer eyJ... (with the space).
  4. Not handling token expiration. Tokens expire. Your client must detect 401 and refresh.
  5. Storing tokens in localStorage for sensitive apps. localStorage is vulnerable to XSS. Use HttpOnly cookies for high-security apps.
  6. Sending the wrong scheme. Authorization: Basic <token> when the API expects Bearer. The server returns 401.

Practical Exercise (10 minutes)

  1. Decode this Base64 string: YW5pdGE6c2VjcmV0MTIz — confirm it's anita:secret123.
  2. Use cURL with -u to call any API that uses Basic Auth (e.g., a test HTTPBin endpoint: curl -u user:pass https://httpbin.org/basic-auth/user/pass).
  3. 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).
  4. 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 Authorization header: <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.
Course continuity
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.

Test Your Knowledge
How did you find this?

Comments

Join the discussion! Sign in with your Google or Blogger account, or comment as Anonymous - no account needed. For quick questions, also reach me on Telegram @cytestch.

Comments