Mapping HTTP Methods to CRUD Operations
Learn how to map HTTP methods to Create, Read, Update, Delete operations when designing RESTful APIs.
REST Patterns
Detailed Explanation
The CRUD-to-HTTP Mapping
REST APIs conventionally map database CRUD (Create, Read, Update, Delete) operations to HTTP methods:
Standard Mapping
| CRUD Operation | HTTP Method | Endpoint Pattern | Example |
|---|---|---|---|
| Create | POST | POST /resources |
POST /api/users |
| Read (list) | GET | GET /resources |
GET /api/users |
| Read (single) | GET | GET /resources/:id |
GET /api/users/42 |
| Update (full) | PUT | PUT /resources/:id |
PUT /api/users/42 |
| Update (partial) | PATCH | PATCH /resources/:id |
PATCH /api/users/42 |
| Delete | DELETE | DELETE /resources/:id |
DELETE /api/users/42 |
URI Design
Resources are nouns, not verbs:
# Good
POST /api/users (create a user)
GET /api/users/42 (get user 42)
PUT /api/users/42 (update user 42)
# Bad
POST /api/createUser
GET /api/getUser?id=42
POST /api/updateUser
Nested Resources
GET /api/users/42/posts (list user 42's posts)
POST /api/users/42/posts (create a post for user 42)
GET /api/users/42/posts/7 (get post 7 of user 42)
DELETE /api/users/42/posts/7 (delete post 7)
Status Code Conventions
| Operation | Success Status | Common Errors |
|---|---|---|
| POST (create) | 201 Created | 400, 409, 422 |
| GET (read) | 200 OK | 404 |
| PUT (replace) | 200 or 204 | 400, 404, 409 |
| PATCH (update) | 200 or 204 | 400, 404, 422 |
| DELETE (remove) | 204 No Content | 404 |
Beyond Basic CRUD
Some operations do not fit neatly into CRUD:
- Search —
GET /api/users/search?q=aliceorPOST /api/users/searchfor complex queries - Bulk operations —
POST /api/users/bulk-deletewith a body of IDs - Actions —
POST /api/orders/42/cancelfor non-CRUD actions
Use Case
A team designing a new REST API maps their database tables to resource endpoints: POST /api/products for creating products, GET /api/products/:id for reading, PATCH /api/products/:id for updating prices, and DELETE /api/products/:id for removing discontinued items.