Skip to content
beginner Phase · HTTP and Web Fundamentals

HTTP Request

Understand the structure and components of an HTTP request.

30m
0 problems
Topic Progress 0%

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

  1. Missing Host header (required in HTTP/1.1)
  2. Wrong Content-Type (causes 415 Unsupported Media Type)
  3. Expired/invalid auth tokens (causes 401 Unauthorized)
  4. 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

  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 / 3 solved
Implement HTTP Request

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

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
HTTP Request Testing Strategy

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?

Question 1 options

2. What header specifies the format of the request body?

Question 2 options

3. What is a common mistake when implementing HTTP Request?

Question 3 options

Flashcards

Question

What are the 4 parts of an HTTP request?

Answer

Request line, Headers, Blank line, Body

Question

What does Content-Type specify?

Answer

The media format of the request body (e.g., application/json)

Question

HTTP Request best practices

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