Skip to content
beginner Phase 1 · Web & Internet Foundations

HTTP Protocol Deep Dive

Master HTTP methods, status codes, headers, cookies, and the request/response model.

1h
0 problems
Topic Progress 0%

HTTP Methods in Depth

HTTP Methods in Depth

HTTP defines several methods (verbs) that indicate the desired action on a resource.

GET — Read a Resource

GET /api/users/123 HTTP/1.1
Host: api.example.com
Accept: application/json
HTTP/1.1 200 OK
Content-Type: application/json

{
  "id": 123,
  "name": "Alice",
  "email": "alice@example.com"
}

GET is safe (doesn't modify data) and idempotent (same result every time). Browsers cache GET responses.

POST — Create a Resource

POST /api/users HTTP/1.1
Content-Type: application/json

{
  "name": "Bob",
  "email": "bob@example.com",
  "password": "securePass123"
}
HTTP/1.1 201 Created
Location: /api/users/124
Content-Type: application/json

{
  "id": 124,
  "name": "Bob",
  "email": "bob@example.com"
}

POST is not idempotent — sending it twice creates two resources. Returns 201 Created with a Location header.

PUT — Replace a Resource

PUT /api/users/123 HTTP/1.1
Content-Type: application/json

{
  "name": "Alice Updated",
  "email": "alice.new@example.com"
}

PUT replaces the entire resource. If a field is missing, it's set to null/default. Always idempotent.

PATCH — Partial Update

PATCH /api/users/123 HTTP/1.1
Content-Type: application/json

{
  "email": "alice.updated@example.com"
}

PATCH only updates the specified fields, leaving others unchanged.

DELETE — Remove a Resource

DELETE /api/users/123 HTTP/1.1
HTTP/1.1 204 No Content

Returns 204 No Content on success. Idempotent — deleting an already-deleted resource returns the same result.

Idempotency Summary

Method Safe Idempotent Use Case
GET Yes Yes Read data
POST No No Create resource
PUT No Yes Replace resource
PATCH No Yes Partial update
DELETE No Yes Remove resource

HTTP Status Codes

HTTP Status Codes

Status codes tell the client what happened with their request.

2xx — Success

Code Name When to Use
200 OK Successful GET, PUT, or PATCH
201 Created Resource created via POST
202 Accepted Request accepted for async processing
204 No Content Successful DELETE, no body returned
// Express.js examples
app.get('/api/users/:id', (req, res) => {
  const user = findUser(req.params.id);
  if (!user) return res.status(404).json({ error: 'User not found' });
  res.json(user);  // 200 OK
});

app.post('/api/users', (req, res) => {
  const user = createUser(req.body);
  res.status(201).json(user);  // 201 Created
});

app.delete('/api/users/:id', (req, res) => {
  deleteUser(req.params.id);
  res.status(204).end();  // 204 No Content
});

3xx — Redirection

Code Name When to Use
301 Moved Permanently URL changed permanently (SEO link juice passes)
302 Found Temporary redirect
304 Not Modified Use cached version (conditional GET)

4xx — Client Error

Code Name When to Use
400 Bad Request Invalid JSON, missing required fields
401 Unauthorized No authentication or invalid token
403 Forbidden Authenticated but not allowed
404 Not Found Resource doesn't exist
409 Conflict Duplicate resource (e.g., email already exists)
422 Unprocessable Entity Validation failed
429 Too Many Requests Rate limit exceeded

5xx — Server Error

Code Name When to Use
500 Internal Server Error Unhandled exception
502 Bad Gateway Upstream server returned invalid response
503 Service Unavailable Server overloaded or down for maintenance

Common Mistakes

  • Using 200 for errors instead of proper 4xx codes
  • Returning 401 when you mean 403 (401 = not logged in, 403 = logged in but not allowed)
  • Not including error details in the response body

HTTP Headers

HTTP Headers

Headers carry metadata about requests and responses.

Content Negotiation

# Client sends preferred formats
Accept: application/json
Accept-Language: en-US,en;q=0.9
Accept-Encoding: gzip, deflate, br

# Server responds with actual format
Content-Type: application/json
Content-Encoding: gzip
Content-Language: en-US

Authentication Headers

# JWT Bearer token
Authorization: Bearer eyJhbGciOiJIUzI1NiIs...

# Basic auth (base64 encoded)
Authorization: Basic dXNlcjpwYXNzd29yZA==

# API key (custom header)
X-API-Key: abc123def456

Caching Headers

# Server tells client how to cache
Cache-Control: public, max-age=3600
ETag: "abc123"
Last-Modified: Wed, 21 Sep 2026 10:00:00 GMT

# Client sends conditional request
If-None-Match: "abc123"
If-Modified-Since: Wed, 21 Sep 2026 10:00:00 GMT

# Server responds with 304 if unchanged
HTTP/1.1 304 Not Modified

CORS Headers

# Server allows cross-origin requests
Access-Control-Allow-Origin: https://myapp.com
Access-Control-Allow-Methods: GET, POST, PUT, DELETE
Access-Control-Allow-Headers: Content-Type, Authorization
Access-Control-Max-Age: 86400

# Preflight request (browser sends automatically)
OPTIONS /api/data HTTP/1.1
Origin: https://myapp.com
Access-Control-Request-Method: POST

Security Headers

Strict-Transport-Security: max-age=31536000; includeSubDomains
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block
Content-Security-Policy: default-src 'self'
Referrer-Policy: strict-origin-when-cross-origin

Cookie Headers

# Server sets a cookie
Set-Cookie: sessionId=abc123; HttpOnly; Secure; SameSite=Strict; Path=/

# Client sends cookie back
Cookie: sessionId=abc123; theme=dark

Key Headers to Know

Header Direction Purpose
Content-Type Both Data format (JSON, HTML, etc.)
Authorization Request Authentication credentials
Cache-Control Response Caching instructions
Set-Cookie Response Store data in browser
CORS headers Response Allow cross-origin requests
Location Response Redirect URL

Quiz

1. Which HTTP method should be used to create a new resource?

Question 1 options

2. What does status code 304 mean?

Question 2 options

3. Which header is used to send authentication credentials?

Question 3 options

Flashcards

Question

What is the difference between PUT and PATCH?

Answer

PUT replaces the entire resource (you must send all fields). PATCH partially updates a resource (you only send the changed fields).

Question

What does idempotent mean in HTTP?

Answer

An idempotent request produces the same result whether sent once or multiple times. GET, PUT, PATCH, DELETE are idempotent. POST is not.

Question

What is the purpose of the Content-Type header?

Answer

Content-Type tells the server/client what format the data is in (application/json, text/html, multipart/form-data, etc.).

Revision Notes

Key Takeaways

  • 1. GET reads, POST creates, PUT replaces, PATCH updates, DELETE removes
  • 2. 2xx = success, 3xx = redirect, 4xx = client error, 5xx = server error
  • 3. Headers carry metadata: auth tokens, content type, caching rules, CORS policies
  • 4. Idempotent methods (GET, PUT, DELETE) can be safely retried

Interview Tips

  • Know when to use POST vs PUT vs PATCH
  • Explain the difference between 401 and 403 status codes
  • Discuss CORS and why it exists
  • Understand how HTTP caching works with Cache-Control headers

Cheat Sheet

HTTP Methods & Status Codes

  • GET: Read (idempotent)
  • POST: Create (not idempotent)
  • PUT: Replace (idempotent)
  • PATCH: Update (idempotent)
  • DELETE: Remove (idempotent)
  • 200 OK: Success
  • 201 Created: Resource created
  • 400 Bad Request: Invalid input
  • 401 Unauthorized: Not authenticated
  • 403 Forbidden: Not authorized
  • 404 Not Found: Resource doesn't exist
  • 500 Server Error: Something broke