Common Request Headers
Request headers provide the server with information about the client and the request.
Essential Request Headers
| Header | Purpose | Example |
|---|---|---|
Host |
Target domain (required in HTTP/1.1) | api.example.com |
User-Agent |
Client software identification | Mozilla/5.0... Chrome/120.0 |
Accept |
Preferred response content types | application/json, text/html |
Accept-Language |
Preferred languages | en-US, en;q=0.9, fr;q=0.8 |
Accept-Encoding |
Compression algorithms | gzip, deflate, br |
Authorization |
Authentication credentials | Bearer eyJhbGciOi... |
Cookie |
Stored cookies | session=abc123; theme=dark |
Content-Type |
Body format | application/json |
Content-Length |
Body size in bytes | 1234 |
Cache-Control |
Caching directives | no-cache, max-age=0 |
If-None-Match |
ETag for conditional requests | "abc123" |
If-Modified-Since |
Date for conditional requests | Sat, 01 Jan 2024 |
Referer |
Previous page URL | https://google.com/search |
Origin |
Request origin (CORS) | https://www.example.com |
Content Negotiation
Clients tell servers what they accept:
Accept: text/html, application/json;q=0.9, */*;q=0.8
| | |
priority 1.0 priority 0.9 priority 0.8
Accept-Language: en-US, en;q=0.9, fr;q=0.8
Accept-Encoding: gzip, deflate, br
Conditional Request Headers
// Only send if resource changed
If-None-Match: "etag-value"
If-Modified-Since: Sat, 01 Jan 2024 00:00:00 GMT
// Send only if conditions met
If-Match: "etag-value"
If-Unmodified-Since: Sat, 01 Jan 2024
If-Range: "etag-value"
Common Response Headers
Response headers control how the browser handles the response.
Essential Response Headers
| Header | Purpose | Example |
|---|---|---|
Content-Type |
Response body format | application/json; charset=utf-8 |
Content-Length |
Body size in bytes | 1234 |
Content-Encoding |
Compression used | gzip |
Set-Cookie |
Store cookies on client | session=abc123; HttpOnly; Secure |
Cache-Control |
Caching instructions | max-age=3600, private |
ETag |
Resource version identifier | "abc123" |
Last-Modified |
When resource was last changed | Sat, 01 Jan 2024 |
Location |
Redirect URL | /new-page |
Access-Control-Allow-Origin |
CORS permission | * |
Server |
Server software | nginx/1.24.0 |
Caching Headers
// Strong caching (browser doesn't check)
Cache-Control: max-age=3600 // Cache for 1 hour
Cache-Control: immutable // Never changes
Expires: Sat, 01 Jan 2025 00:00:00 // Deprecated
// Validation (browser checks with server)
Cache-Control: no-cache // Must revalidate
ETag: "abc123" // Version identifier
Last-Modified: Sat, 01 Jan 2024 // Last change date
// No caching
Cache-Control: no-store // Don't cache at all
Cache-Control: no-cache, no-store // Maximum freshness
Security Headers
Strict-Transport-Security: max-age=31536000; includeSubDomains
// Force HTTPS for 1 year
X-Content-Type-Options: nosniff
// Prevent MIME type sniffing
X-Frame-Options: DENY
// Prevent clickjacking (iframe)
X-XSS-Protection: 1; mode=block
// Enable XSS filter (legacy)
Content-Security-Policy: default-src 'self'
// Restrict resource loading
Referrer-Policy: strict-origin-when-cross-origin
// Control referrer information
Permissions-Policy: camera=(), microphone=()
// Restrict browser features
Custom Headers
Custom headers extend HTTP for application-specific needs.
Custom Header Convention
Custom headers typically use the X- prefix (deprecated but still common):
X-Request-ID: 123e4567-e89b-12d3-a456-426614174000
X-User-ID: 12345
X-Client-Version: 2.1.0
X-Forwarded-For: 203.0.113.195
Common Custom Headers
| Header | Purpose |
|---|---|
X-Request-ID |
Request tracing across services |
X-User-ID |
Authenticated user identifier |
X-Forwarded-For |
Client IP (behind proxy) |
X-Real-IP |
Actual client IP |
X-RateLimit-Limit |
Rate limit maximum |
X-RateLimit-Remaining |
Requests remaining |
X-RateLimit-Reset |
When limit resets |
API Versioning Headers
// Header-based versioning
Accept: application/vnd.api.v2+json
API-Version: 2
X-API-Version: 2
// Response headers
API-Version: 2
Deprecation: true
Sunset: Sat, 01 Jan 2025
Debugging Headers
X-Request-ID: abc-123-def-456
X-Response-Time: 45ms
X-Served-By: server-03
X-Cache: HIT
X-Cache-Hits: 5
Reading Custom Headers in JavaScript
// Fetch API
const response = await fetch('/api/data');
const requestId = response.headers.get('X-Request-ID');
const rateLimit = response.headers.get('X-RateLimit-Remaining');
// List all headers
response.headers.forEach((value, key) => {
console.log(`${key}: ${value}`);
});
// Sending custom headers
fetch('/api/data', {
headers: {
'X-Request-ID': generateUUID(),
'X-User-ID': userId,
'Authorization': `Bearer ${token}`
}
});
Practice Problems
Create a reusable React component implementing HTTP Headers. Include proper state management and accessibility.
Solution
// Production-ready component with:
// - Proper TypeScript types
// - Accessibility (ARIA)
// - Error boundaries
// - Loading states
// - Memoization where neededWrite unit and integration tests for HTTP Headers using React Testing Library.
Solution
// Test coverage:
// 1. Rendering tests
// 2. Interaction tests
// 3. Edge case tests
// 4. Accessibility testsOptimize HTTP Headers for performance. Consider memoization, code splitting, and bundle size.
Solution
// Optimization techniques:
// 1. React.memo / useMemo / useCallback
// 2. Code splitting with lazy()
// 3. Virtual scrolling for lists
// 4. Image lazy loading
// 5. Bundle analysisQuiz
1. Which header forces HTTPS for a year?
2. What does Cache-Control: no-cache mean?
3. Which header prevents clickjacking?
4. What is the purpose of the X-Request-ID header?
Flashcards
Question
What are the three caching strategies in Cache-Control?
Click to reveal answer
Answer
max-age (strong cache), no-cache (revalidate), no-store (no caching at all).
Question
What security headers should every website use?
Click to reveal answer
Answer
HSTS, X-Content-Type-Options, X-Frame-Options, CSP, Referrer-Policy.
Question
What is content negotiation?
Click to reveal answer
Answer
Client tells server preferred format via Accept, Accept-Language, Accept-Encoding headers.
Question
What does ETag do?
Click to reveal answer
Answer
Provides a version identifier for a resource. Used with If-None-Match for conditional requests and caching.
Question
What is HTTP Headers?
Click to reveal answer
Answer
HTTP Headers is a key concept in frontend development.
Revision Notes
Key Takeaways
- 1.Headers carry metadata that controls request/response behavior
- 2.Caching headers significantly impact performance
- 3.Security headers protect against common attacks
- 4.Custom headers enable API versioning and debugging
- 5.Content negotiation lets clients specify preferred formats
Interview Tips
- •Know the most important security headers
- •Understand caching strategies and when to use each
- •Explain content negotiation with Accept headers
- •Know how to trace requests using X-Request-ID
Cheat Sheet
HTTP Headers Cheat Sheet
Request Headers:
- Host: Target domain
- Accept: Preferred format
- Authorization: Auth credentials
- Cookie: Stored cookies
- If-None-Match: Conditional request
Response Headers:
- Content-Type: Body format
- Set-Cookie: Store cookies
- Cache-Control: Caching rules
- Location: Redirect URL
- ETag: Resource version
Caching:
- max-age=N: Cache for N seconds
- no-cache: Must revalidate
- no-store: Don't cache
Security Headers:
- HSTS: Force HTTPS
- CSP: Restrict resources
- X-Frame-Options: Prevent iframe
- X-Content-Type-Options: No MIME sniff