HTTP Response Structure
An HTTP response mirrors the request structure:
HTTP/1.1 200 OK ← Status Line
Content-Type: application/json ← Headers
Cache-Control: max-age=3600
X-Request-Id: abc-123
← Blank Line
{ ← Body
"id": 123,
"name": "Alice",
"email": "alice@example.com"
}
1. Status Line
HTTP/1.1 200 OK
│ │ │
│ │ └── Reason Phrase (human-readable)
│ └────────── Status Code (numeric)
└─────────────────── HTTP Version
2. Response Headers
| Header | Purpose |
|---|---|
| Content-Type | Format of response body |
| Content-Length | Size of response body in bytes |
| Cache-Control | Caching directives |
| Set-Cookie | Set cookies on client |
| Location | Redirect URL |
| ETag | Resource version for caching |
| Access-Control-Allow-Origin | CORS permissions |
3. Status Code Categories
1xx: Informational → Processing, Switching Protocols
2xx: Success → OK, Created, No Content
3xx: Redirection → Moved Permanently, Not Modified
4xx: Client Error → Bad Request, Not Found, Forbidden
5xx: Server Error → Internal Error, Bad Gateway
Real-World Response
HTTP/1.1 201 Created
Content-Type: application/json
Location: /api/orders/456
X-Request-Id: req-789
Cache-Control: no-cache
{
"orderId": "456",
"status": "CONFIRMED",
"total": 59.99,
"estimatedDelivery": "2026-08-20"
}
Response Body Formats
| Format | Content-Type | Use Case |
|---|---|---|
| JSON | application/json | API responses |
| HTML | text/html | Web pages |
| XML | application/xml | Legacy APIs |
| Text | text/plain | Simple messages |
| Binary | application/octet-stream | File downloads |
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 Response 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 Response in a backend system. Consider scalability, error handling, and production readiness.
Solution
// HTTP Response implementation
// Key aspects: validation, error handling, logging, testing
public class HTTPResponse {
// Production-ready implementation
}Identify and handle edge cases for HTTP Response. 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, idempotencyWrite a testing strategy for HTTP Response. Include unit tests, integration tests, and performance tests.
Solution
// Test plan:
// - Unit: 80% coverage target
// - Integration: API contracts
// - Performance: latency, throughput
// - Chaos: failure injectionQuiz
1. What does the HTTP status code 201 indicate?
2. Which response header sets cookies on the client?
3. What is the primary purpose of HTTP Response?
4. What is a common mistake when implementing HTTP Response?
Flashcards
Question
What are the 3 parts of an HTTP response?
Click to reveal answer
Answer
Status line, Headers, Body
Question
What does status code 200 mean?
Click to reveal answer
Answer
OK — the request was successful
Question
What is HTTP Response?
Click to reveal answer
Answer
HTTP Response is a key concept in backend development.
Question
When to use HTTP Response?
Click to reveal answer
Answer
Use HTTP Response when building production systems that require reliability, scalability, and maintainability.
Question
HTTP Response 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 response = Status Line + Headers + Body
- 2.Status line: HTTP version + status code + reason phrase
- 3.2xx = success, 4xx = client error, 5xx = server error
- 4.Headers carry metadata (content-type, caching, cookies)
Interview Tips
- •Know the meaning of common status codes
- •Understand response header purposes
Cheat Sheet
HTTP Response
- Status Line: HTTP/1.1 + Code + Reason
- Headers: Content-Type, Cache-Control, Set-Cookie
- Categories: 1xx=Info, 2xx=OK, 3xx=Redirect, 4xx=Client Error, 5xx=Server Error