Basic authentication helps a server check who you are by asking for a username and password. It keeps things simple and quick.
Basic authentication in Rest API
Start learning this pattern below
Jump into concepts and practice - no test required
Authorization: Basic base64(username:password)
The username and password are joined by a colon and then encoded in base64.
This header is sent with each request to prove who you are.
Authorization: Basic dXNlcjpwYXNz
Authorization: Basic YWRtaW46MTIzNA==
This program shows how to send a request with Basic authentication. It encodes the username and password, adds them to the header, and calls a test API that requires these credentials.
import base64 import requests username = 'user' password = 'pass' # Combine username and password user_pass = f'{username}:{password}' # Encode to base64 encoded = base64.b64encode(user_pass.encode()).decode() # Prepare headers with Basic Auth headers = {'Authorization': f'Basic {encoded}'} # Example URL (replace with real API endpoint) url = 'https://httpbin.org/basic-auth/user/pass' # Send GET request with headers response = requests.get(url, headers=headers) # Print status and response JSON print('Status code:', response.status_code) print('Response:', response.json())
Basic authentication sends credentials in base64, which is not encrypted. Use HTTPS to keep it safe.
Each request must include the Authorization header with the encoded credentials.
For better security, consider more advanced methods like token-based authentication.
Basic authentication uses a username and password encoded in base64 sent in the Authorization header.
It is simple but should be used only over secure connections (HTTPS).
It is useful for quick and easy protection of APIs or services.
Practice
Solution
Step 1: Understand Basic Authentication mechanism
Basic Authentication sends a username and password encoded in base64 in the Authorization header.Step 2: Compare with other authentication methods
API keys, OAuth tokens, and IP filtering are different methods, not Basic Authentication.Final Answer:
A username and password encoded in base64 sent in the Authorization header -> Option BQuick Check:
Basic Auth = username:password base64 in header [OK]
- Confusing Basic Auth with API key or OAuth
- Thinking credentials are sent in URL or body
- Ignoring base64 encoding step
Solution
Step 1: Recall the header format for Basic Authentication
The header must start with the word 'Basic' followed by a space and then the base64 encoded credentials.Step 2: Eliminate other header types
'Bearer', 'Token', and 'ApiKey' are used in other authentication schemes, not Basic Auth.Final Answer:
Authorization: Basic base64encodedstring -> Option DQuick Check:
Basic Auth header starts with 'Basic' [OK]
- Using 'Bearer' instead of 'Basic'
- Omitting the space after 'Basic'
- Confusing with other auth schemes
Solution
Step 1: Combine username and password with colon
Combine 'user' and 'pass' as 'user:pass'.Step 2: Encode 'user:pass' in base64
Encoding 'user:pass' in base64 results in 'dXNlcjpwYXNzdA=='.Final Answer:
Authorization: Basic dXNlcjpwYXNzdA== -> Option CQuick Check:
Base64('user:pass') = dXNlcjpwYXNzdA== [OK]
- Encoding username and password separately
- Adding extra characters or padding incorrectly
- Using wrong base64 string
Authorization: Basic user:passSolution
Step 1: Check the format of the Authorization header
The header must have the credentials base64 encoded after 'Basic '.Step 2: Identify the error in the given header
The given header has 'user:pass' in plain text, not base64 encoded.Final Answer:
The username and password are not base64 encoded -> Option AQuick Check:
Basic Auth requires base64 encoding [OK]
- Sending plain text credentials
- Confusing 'Basic' with 'Bearer'
- Misplacing colon or other punctuation
Solution
Step 1: Understand security risks of Basic Authentication
Basic Auth sends credentials encoded but not encrypted, so it must be used over HTTPS to protect data.Step 2: Identify best practice for secure API protection
Using HTTPS encrypts the entire connection, making base64 encoded credentials safe to transmit.Final Answer:
Use HTTPS to encrypt the connection and send base64 encoded credentials in the Authorization header -> Option AQuick Check:
Basic Auth + HTTPS = secure transmission [OK]
- Sending credentials over HTTP (not secure)
- Putting credentials in URL parameters
- Skipping base64 encoding
