REST Principles
Resource-based, Stateless, Cacheable, Uniform interface, Layered।
HTTP Methods
| Method | Use | Idempotent |
|---|---|---|
| GET | Retrieve | ✅ |
| POST | Create | ❌ |
| PUT | Replace | ✅ |
| PATCH | Partial | ❌ |
| DELETE | Remove | ✅ |
Status Codes
- 2xx — 200, 201, 204
- 3xx — 301, 304
- 4xx — 400, 401, 403, 404, 409, 422, 429
- 5xx — 500, 502, 503
Resource Naming ⭐
✅ /api/users (plural) ✅ /api/users/123/orders ❌ /api/getUsers (verb) ❌ /api/user_list
Filtering, Pagination
GET /api/products?category=electronics&minPrice=100 GET /api/products?sort=price&order=desc GET /api/products?page=2&limit=20 GET /api/products?fields=id,name (sparse)
Versioning
- URL: /api/v1/users (popular)
- Header: Accept: application/vnd.app.v2+json
- Query: ?version=2
Response Format
{
"data": [...],
"meta": {"page": 1, "total": 250},
"links": {"next": "/api/users?page=2"}
}
// Error
{
"error": {
"code": "VALIDATION_FAILED",
"message": "Invalid input",
"details": [{"field": "email", "msg": "Invalid"}]
},
"request_id": "abc-123"
}
HATEOAS (Maturity L3)
{
"id": 1,
"_links": {
"self": {"href": "/users/1"},
"orders": {"href": "/users/1/orders"}
}
}
Best Practices
- Consistent naming
- UTC ISO 8601 timestamp
- HTTPS, gzip
- Idempotency-Key header for POST
- ETag caching
- OpenAPI/Swagger doc