2.8 KiB
2.8 KiB
HTTP Methods and Status Codes Reference
HTTP Methods
| Method | Idempotent | Safe | Purpose | Typical Status |
|---|---|---|---|---|
| GET | Yes | Yes | Retrieve resource | 200, 304, 404 |
| POST | No | No | Create resource | 201, 400, 409 |
| PUT | Yes | No | Replace resource | 200, 204, 404 |
| PATCH | No | No | Partial update | 200, 204, 400 |
| DELETE | Yes | No | Remove resource | 204, 404 |
| HEAD | Yes | Yes | Like GET, no body | 200, 304, 404 |
| OPTIONS | Yes | Yes | Describe communication options | 200 |
Idempotent Operations
An operation is idempotent if making the same request multiple times produces the same result as making it once.
Safe Operations
A safe operation doesn't change the state of the server. Safe operations are always idempotent.
HTTP Status Codes
2xx Success
200 OK: Successful GET/PUT/PATCH201 Created: Successful POST (include Location header)202 Accepted: Async processing accepted204 No Content: Successful DELETE or POST with no content206 Partial Content: Range request successful
3xx Redirection
301 Moved Permanently: Resource permanently moved304 Not Modified: Cache valid, use local copy307 Temporary Redirect: Temporary redirect
4xx Client Errors
400 Bad Request: Invalid format or parameters401 Unauthorized: Authentication required403 Forbidden: Authenticated but not authorized404 Not Found: Resource doesn't exist409 Conflict: Constraint violation or conflict422 Unprocessable Entity: Validation failed (semantic error)429 Too Many Requests: Rate limit exceeded
5xx Server Errors
500 Internal Server Error: Unexpected server error502 Bad Gateway: External service unavailable503 Service Unavailable: Server temporarily down504 Gateway Timeout: External service timeout
Common REST API Patterns
Resource URLs
GET /users # List all users
GET /users/123 # Get specific user
POST /users # Create user
PUT /users/123 # Update user
DELETE /users/123 # Delete user
GET /users/123/orders # Get user's orders
Query Parameters
GET /users?page=0&size=20&sort=createdAt,desc
- page: Page number (0-based)
- size: Number of items per page
- sort: Sorting format (field,direction)
Response Headers
Location: /api/users/123 # For 201 Created responses
X-Total-Count: 45 # Total items count
Cache-Control: no-cache # Cache control
Content-Type: application/json # Response format
Error Response Format
{
"status": 400,
"error": "Bad Request",
"message": "Validation failed: name: Name cannot be blank, email: Valid email required",
"path": "/api/users",
"timestamp": "2024-01-15T10:30:00Z"
}