HTTP Request Structure
An HTTP request consists of four parts:
GET /api/users?active=true HTTP/1.1 ← Request Line
Host: api.example.com ← Headers
Authorization: Bearer eyJhbG...
Accept: application/json
← Blank Line
{"name": "Alice"} ← Body (optional)
1. Request Line
GET /api/users?active=true HTTP/1.1
│ │ │ │
│ │ │ └── Protocol Version
│ │ └────────── Query Parameters
│ └─────────────────────────── Path
└──────────────────────────────── Method (Verb)
2. Headers
Headers carry metadata about the request:
| Header | Purpose |
|---|---|
| Host | Target domain (required in HTTP/1.1) |
| Authorization | Authentication credentials |
| Accept | Accepted response formats |
| Content-Type | Format of request body |
| User-Agent | Client software info |
| Cache-Control | Caching directives |
| Cookie | Session cookies |
3. Query Parameters
GET /api/search?q=node&page=2&limit=20
________________________/ ____________/
Base Path Parameters
?q=node&page=2&limit=20
│ │ │
│ │ └── limit=20
│ └────────── page=2
└────────────────── q=node
4. Request Body
The body carries data sent to the server:
POST /api/users HTTP/1.1
Content-Type: application/json
{
"name": "Alice",
"email": "alice@example.com",
"role": "admin"
}
Real-World Example
POST /api/orders HTTP/1.1
Host: api.amazon.com
Authorization: Bearer eyJhbGciOiJSUzI1NiIs...
Content-Type: application/json
Accept: application/json
User-Agent: MyApp/2.0
X-Request-ID: abc-123-def
X-Forwarded-For: 203.0.113.50
{
"items": [
{"productId": "B07N199W1Z", "quantity": 2},
{"productId": "B08N5WRWNW", "quantity": 1}
],
"shippingMethod": "PRIME",
"paymentMethod": "CREDIT_CARD"
}
Common Request Mistakes
- Missing Host header (required in HTTP/1.1)
- Wrong Content-Type (causes 415 Unsupported Media Type)
- Expired/invalid auth tokens (causes 401 Unauthorized)
- Incorrect query parameter encoding
HTTP Best Practices
Methods
- GET: Read (safe, idempotent)
- POST: Create
- PUT: Replace (idempotent)
- PATCH: Partial update
- DELETE: Remove (idempotent)
Headers
- Content-Type: Body format
- Cache-Control: Caching rules
- Authorization: Authentication
- Accept: Desired response format
Status Codes
- 2xx: Success
- 3xx: Redirection
- 4xx: Client error
- 5xx: Server error
Key Points
- Understanding HTTP Request is essential for production systems
- Always consider scalability and maintainability
- Test thoroughly before deploying to production
- Monitor performance and set up alerting
Common Patterns
- Validation: Always validate input at the boundary
- Error Handling: Use structured error responses
- Logging: Log key events for debugging
- Testing: Unit, integration, and load tests
- Documentation: Keep docs updated with code changes
Practice Problems
Design and implement a solution for HTTP Request in a backend system. Consider scalability, error handling, and production readiness.
Solution
// HTTP Request implementation
// Key aspects: validation, error handling, logging, testing
public class HTTPRequest {
// Production-ready implementation
} Identify and handle edge cases for HTTP Request. What happens under high load, with invalid input, or during failures?
Solution
// Edge case handling:
// 1. Null/empty input -> validation
// 2. High load -> rate limiting, queuing
// 3. Failures -> retries, circuit breaker
// 4. Concurrent access -> locks, idempotency Write a testing strategy for HTTP Request. Include unit tests, integration tests, and performance tests.
Solution
// Test plan:
// - Unit: 80% coverage target
// - Integration: API contracts
// - Performance: latency, throughput
// - Chaos: failure injection Quiz
1. Which part of an HTTP request is required?
2. What header specifies the format of the request body?
3. What is a common mistake when implementing HTTP Request?
Flashcards
Question
What are the 4 parts of an HTTP request?
Click to reveal answer
Answer
Request line, Headers, Blank line, Body
Question
What does Content-Type specify?
Click to reveal answer
Answer
The media format of the request body (e.g., application/json)
Question
HTTP Request best practices
Click to reveal answer
Answer
Follow SOLID principles, write clean code, test thoroughly, document decisions, and monitor in production.
Revision Notes
Key Takeaways
- 1. HTTP request = Request Line + Headers + Body
- 2. Request line contains: method, path, HTTP version
- 3. Headers carry metadata (auth, content-type, etc.)
- 4. Body carries data (POST/PUT payloads)
Interview Tips
- • Be able to construct an HTTP request from scratch
- • Know the difference between Content-Type and Accept
Cheat Sheet
HTTP Request
- Request Line: Method + Path + HTTP Version
- Headers: Host, Auth, Content-Type, Accept
- Body: POST/PUT data (JSON, form-data)
- Query Params: ?key=value&key2=value2