๐ HTTP Headers with RapidTest¶
Introduction to HTTP headers, learn when and how to use the most important ones in your API. โก
๐ Overview¶
HTTP headers provide essential metadata about requests and responses, headers exist for authentication, content negotiation, and more.
1. ๐ Authorization Header¶
๐ฏ Purpose¶
- Authenticate and authorize API requests.
๐ก When to Use¶
- Protected endpoints
- User authentication
๐ Examples¶
Bearer Token¶
from rapidtest import HTTPTest
# Test with Bearer token
test = HTTPTest(url="http://localhost:8000")
test.get(
path="/protected-endpoint",
headers={"Authorization": "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOi..."}
)
2. ๐ Content-Type Header¶
๐ฏ Purpose¶
- Specify the format of request/response body.
๐ก When to Use¶
- Sending JSON data
- Form submissions
- File uploads
๐ Examples¶
JSON Data¶
# Sending JSON data
data = {"name": "John", "email": "john@example.com"}
test.post(
path="/users",
json=data, # RapidTest automatically sets Content-Type: application/json
headers={"Content-Type": "application/json"}
)
3. ๐ช Cookie Headers¶
๐ฏ Purpose¶
- Manage session state and user preferences.
๐ก When to Use¶
- Session management
- User preferences
- Authentication tokens
๐ Examples¶
Sending Cookies¶
# Test with session cookie
test.get(
path="/dashboard",
headers={
"Cookie": "session_id=abc123; user_pref=dark_mode"
}
)
๐งช Testing Headers with RapidTest¶
RapidTest makes testing HTTP headers incredibly simple with its intuitive syntax. You can easily verify both request headers and response headers. โจ
๐ฏ What's Next?¶
- ๐ Check the Learn About Threads
- ๐ Read the Watch The Tutorials