HTTP Methods Overview
HTTP methods define the action to be performed on a resource. Each method has specific semantics:
CRUD Operations Mapping:
┌──────────┬─────────────┬───────────┬──────────┐
│ Operation│ HTTP Method │ Idempotent│ Safe │
├──────────┼─────────────┼───────────┼──────────┤
│ Create │ POST │ No │ No │
│ Read │ GET │ Yes │ Yes │
│ Update │ PUT │ Yes │ No │
│ Update │ PATCH │ No │ No │
│ Delete │ DELETE │ Yes │ No │
└──────────┴─────────────┴───────────┴──────────┘
Safety and Idempotency
Safe — Doesn't modify the server state (GET, HEAD, OPTIONS)
Idempotent — Same request produces same result (GET, PUT, DELETE)
GET /users/123 → Safe (read only)
POST /users → Not safe, not idempotent
PUT /users/123 → Not safe, but idempotent
DELETE /users/123 → Not safe, but idempotent
PATCH /users/123 → Not safe, not idempotent
Method Comparison
| Method | Body | Cacheable | Idempotent | Safe |
|---|---|---|---|---|
| GET | No | Yes | Yes | Yes |
| HEAD | No | Yes | Yes | Yes |
| POST | Yes | Conditional | No | No |
| PUT | Yes | No | Yes | No |
| PATCH | Yes | No | No | No |
| DELETE | Yes | No | Yes | No |
| OPTIONS | No | No | Yes | Yes |
RESTful API Design
GET /api/products → List all products
GET /api/products/123 → Get product 123
POST /api/products → Create new product
PUT /api/products/123 → Replace product 123
PATCH /api/products/123 → Update fields of product 123
DELETE /api/products/123 → Delete product 123
Why Method Semantics Matter
Using the wrong method causes problems:
- Using GET for mutations — Browsers prefetch/crawl GET URLs
- Using POST for idempotent ops — Retries create duplicates
- Using PUT instead of PATCH — Must send full resource every time
Implementation Details
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 Methods 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 Methods in a backend system. Consider scalability, error handling, and production readiness.
Solution
// HTTP Methods implementation
// Key aspects: validation, error handling, logging, testing
public class HTTPMethods {
// Production-ready implementation
}Identify and handle edge cases for HTTP Methods. 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 Methods. 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. Which HTTP methods are considered safe (don't modify server state)?
2. Why is PUT idempotent but POST is not?
3. What is the primary purpose of HTTP Methods?
4. What is a common mistake when implementing HTTP Methods?
Flashcards
Question
What are the 5 main HTTP methods?
Click to reveal answer
Answer
GET, POST, PUT, PATCH, DELETE
Question
What does idempotent mean?
Click to reveal answer
Answer
Same request produces same result — GET, PUT, DELETE are idempotent
Question
What is HTTP Methods?
Click to reveal answer
Answer
HTTP Methods is a key concept in backend development.
Question
When to use HTTP Methods?
Click to reveal answer
Answer
Use HTTP Methods when building production systems that require reliability, scalability, and maintainability.
Question
HTTP Methods 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 methods define the action on a resource
- 2.Safe: GET, HEAD, OPTIONS (no server mutation)
- 3.Idempotent: GET, PUT, DELETE (same result on retry)
- 4.REST maps CRUD to GET, POST, PUT/PATCH, DELETE
Interview Tips
- •Explain why idempotency matters for retries
- •Know when to use PUT vs PATCH
Cheat Sheet
HTTP Methods
- GET: Read (safe, idempotent)
- POST: Create (not safe, not idempotent)
- PUT: Replace (idempotent)
- PATCH: Partial update (not idempotent)
- DELETE: Remove (idempotent)