
CRUD Operations: Mapping HTTP Methods to Data Operations
Master the CRUD-to-HTTP mapping with detailed examples for every operation. Build complete API workflows for creating, reading, updating, and deleting data.
CRUD Operations: Mapping HTTP Methods to Data Operations
CRUD — Create, Read, Update, Delete — is the foundation of every data-driven application. REST maps these four operations to HTTP methods in a clean, consistent pattern. In this lesson, we will walk through the complete CRUD lifecycle with detailed examples you can run against live APIs.
1. The CRUD Lifecycle
Every resource in a REST API goes through a lifecycle:
graph LR
A["CREATE<br/>POST /items"] --> B["READ<br/>GET /items/1"]
B --> C["UPDATE<br/>PUT or PATCH /items/1"]
C --> D["DELETE<br/>DELETE /items/1"]
D --> E["Gone<br/>404 on GET /items/1"]
style A fill:#059669,color:#fff
style B fill:#0891b2,color:#fff
style C fill:#d97706,color:#fff
style D fill:#dc2626,color:#fff
style E fill:#6b7280,color:#fff
The Complete Mapping
| CRUD | HTTP Method | URL Pattern | Request Body | Response Code |
|---|---|---|---|---|
| Create | POST | /resources | New resource data | 201 Created |
| Read All | GET | /resources | None | 200 OK |
| Read One | GET | /resources/:id | None | 200 OK |
| Full Update | PUT | /resources/:id | Complete resource | 200 OK |
| Partial Update | PATCH | /resources/:id | Changed fields only | 200 OK |
| Delete | DELETE | /resources/:id | None | 204 No Content |
2. Create (POST)
When to Use POST
- Adding a new user to the system
- Creating a blog post
- Placing an order
- Submitting a form
Complete Example
curl -X POST https://jsonplaceholder.typicode.com/posts \
-H "Content-Type: application/json" \
-d '{
"title": "Understanding CRUD",
"body": "CRUD maps perfectly to HTTP methods in REST.",
"userId": 1
}'
Response (201 Created):
{
"title": "Understanding CRUD",
"body": "CRUD maps perfectly to HTTP methods in REST.",
"userId": 1,
"id": 101
}
What the Server Does
- Validates the input data
- Generates a unique ID
- Sets default values (timestamps, status)
- Inserts into the database
- Returns the complete resource with its new ID
POST Best Practices
| Practice | Description |
|---|---|
| Return the created resource | Include it in the response body |
| Return 201 Created | Not 200 OK |
| Include Location header | URL of the new resource |
| Validate input | Return 400 for invalid data |
| Set defaults server-side | Timestamps, status, generated fields |
3. Read (GET)
Reading Collections
# All posts
curl https://jsonplaceholder.typicode.com/posts
# Filtered posts (by user)
curl "https://jsonplaceholder.typicode.com/posts?userId=1"
Collection responses return an array:
[
{"id": 1, "title": "First Post", "userId": 1},
{"id": 2, "title": "Second Post", "userId": 1},
...
]
Reading Individual Resources
curl https://jsonplaceholder.typicode.com/posts/1
Individual responses return a single object:
{
"userId": 1,
"id": 1,
"title": "sunt aut facere...",
"body": "quia et suscipit..."
}
Reading Related Resources
# Comments on post 1
curl https://jsonplaceholder.typicode.com/posts/1/comments
# Todos for user 3
curl https://jsonplaceholder.typicode.com/users/3/todos
Advanced Reading Patterns
# Pagination
curl "https://api.example.com/products?page=2&limit=10"
# Sorting
curl "https://api.example.com/products?sort=price&order=asc"
# Searching
curl "https://api.example.com/products?q=laptop"
# Multiple filters
curl "https://api.example.com/products?category=electronics&inStock=true&maxPrice=500"
4. Update (PUT and PATCH)
Full Update with PUT
PUT replaces the entire resource:
curl -X PUT https://jsonplaceholder.typicode.com/posts/1 \
-H "Content-Type: application/json" \
-d '{
"id": 1,
"title": "Completely New Title",
"body": "Completely new body content replacing everything.",
"userId": 1
}'
Partial Update with PATCH
PATCH changes only specified fields:
curl -X PATCH https://jsonplaceholder.typicode.com/posts/1 \
-H "Content-Type: application/json" \
-d '{"title": "Only Title Changed"}'
Side-by-Side Comparison
graph TD
subgraph "Original Resource"
O["id: 1<br/>title: 'Hello'<br/>body: 'World'<br/>status: 'draft'"]
end
subgraph "After PUT {title:'New'}"
P["id: 1<br/>title: 'New'<br/>body: null<br/>status: null"]
end
subgraph "After PATCH {title:'New'}"
Q["id: 1<br/>title: 'New'<br/>body: 'World'<br/>status: 'draft'"]
end
O --> P
O --> Q
style O fill:#4f46e5,color:#fff
style P fill:#d97706,color:#fff
style Q fill:#059669,color:#fff
PUT with only title might set body and status to null (they were not included). PATCH with only title leaves body and status untouched.
5. Delete (DELETE)
curl -X DELETE https://jsonplaceholder.typicode.com/posts/1
Delete Response Patterns
| Pattern | Status | Body |
|---|---|---|
| No content | 204 No Content | Empty |
| Confirmation | 200 OK | {"deleted": true} |
| Return deleted resource | 200 OK | The deleted resource data |
6. Complete CRUD Workflow
Let us perform a complete lifecycle on a single resource:
# 1. CREATE a post
curl -X POST https://jsonplaceholder.typicode.com/posts \
-H "Content-Type: application/json" \
-d '{"title":"My Post","body":"Original content","userId":1}'
# Response: 201 Created, id: 101
# 2. READ the post
curl https://jsonplaceholder.typicode.com/posts/101
# Response: 200 OK
# 3. UPDATE the post (partial)
curl -X PATCH https://jsonplaceholder.typicode.com/posts/101 \
-H "Content-Type: application/json" \
-d '{"title":"Updated Post"}'
# Response: 200 OK
# 4. DELETE the post
curl -X DELETE https://jsonplaceholder.typicode.com/posts/101
# Response: 200 OK
# 5. TRY TO READ (should fail)
curl https://jsonplaceholder.typicode.com/posts/101
# Response: 404 Not Found (in a real API)
7. Error Handling in CRUD
Each CRUD operation has common errors:
| Operation | Error | Status | Cause |
|---|---|---|---|
| Create | Missing required field | 400 | Body missing data |
| Create | Duplicate entry | 409 | Resource already exists |
| Read | Resource not found | 404 | Wrong ID or deleted |
| Update | Resource not found | 404 | Wrong ID |
| Update | Invalid data | 422 | Bad field values |
| Delete | Resource not found | 404 | Already deleted |
| Any | Not authenticated | 401 | Missing token |
| Any | Not authorized | 403 | Insufficient role |
Summary and Key Takeaways
- CRUD maps to HTTP: Create=POST, Read=GET, Update=PUT/PATCH, Delete=DELETE.
- POST creates and returns 201; GET reads and returns 200.
- PUT replaces entirely; PATCH updates only specified fields.
- DELETE removes resources and returns 204 (no content) or 200.
- Every CRUD operation has expected error responses you should handle.
- A complete resource lifecycle goes: Create → Read → Update → Delete.
Lesson Review Quiz
?Knowledge Check
You need to change just the email of user 42. Which method should you use?
?Knowledge Check
What status code should a server return after successfully creating a resource with POST?
?Knowledge Check
What happens if you GET a resource that has been deleted?