Skip to content
beginnerPhase ·

HTTP Response

Learn how servers construct and send HTTP responses.

30m
0 problems
Topic Progress0%

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

  1. Validation: Always validate input at the boundary
  2. Error Handling: Use structured error responses
  3. Logging: Log key events for debugging
  4. Testing: Unit, integration, and load tests
  5. Documentation: Keep docs updated with code changes

Practice Problems

0/3solved
Implement HTTP Response

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
}
HTTP Response Edge Cases

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, idempotency
HTTP Response Testing Strategy

Write 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 injection

Quiz

1. What does the HTTP status code 201 indicate?

Question 1 options

2. Which response header sets cookies on the client?

Question 2 options

3. What is the primary purpose of HTTP Response?

Question 3 options

4. What is a common mistake when implementing HTTP Response?

Question 4 options

Flashcards

Question

What are the 3 parts of an HTTP response?

Answer

Status line, Headers, Body

Question

What does status code 200 mean?

Answer

OK — the request was successful

Question

What is HTTP Response?

Answer

HTTP Response is a key concept in backend development.

Question

When to use HTTP Response?

Answer

Use HTTP Response when building production systems that require reliability, scalability, and maintainability.

Question

HTTP Response best practices

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