CRUD Operations with HTTP
CRUD (Create, Read, Update, Delete) operations are the four basic functions of persistent storage. REST maps these operations to HTTP methods in a standardized way.
CRUD to HTTP Mapping
| CRUD | HTTP Method | URI Pattern | Description |
|---|---|---|---|
| Create | POST | /resources | Create a new resource |
| Read | GET | /resources | List all resources |
| Read | GET | /resources/\{id\} | Get a specific resource |
| Update | PUT | /resources/\{id\} | Replace a resource |
| Update | PATCH | /resources/\{id\} | Partially update a resource |
| Delete | DELETE | /resources/\{id\} | Remove a resource |
Complete CRUD Example: User Management
Let's walk through a complete CRUD implementation for a user resource.
Create (POST)
POST /api/users HTTP/1.1
Content-Type: application/json
{
"name": "Alice Smith",
"email": "alice@example.com",
"department": "Engineering"
}
Success Response:
HTTP/1.1 201 Created
Location: /api/users/42
Content-Type: application/json
{
"id": 42,
"name": "Alice Smith",
"email": "alice@example.com",
"department": "Engineering",
"createdAt": "2024-01-15T10:30:00Z"
}
Read Collection (GET)
GET /api/users HTTP/1.1
Accept: application/json
Response:
HTTP/1.1 200 OK
Content-Type: application/json
{
"data": [
{ "id": 1, "name": "Alice Smith", "email": "alice@example.com" },
{ "id": 2, "name": "Bob Jones", "email": "bob@example.com" }
],
"total": 2
}
Read Single Resource (GET)
GET /api/users/42 HTTP/1.1
Accept: application/json
Response:
HTTP/1.1 200 OK
Content-Type: application/json
{
"id": 42,
"name": "Alice Smith",
"email": "alice@example.com",
"department": "Engineering",
"createdAt": "2024-01-15T10:30:00Z"
}
Update - Full Replace (PUT)
PUT /api/users/42 HTTP/1.1
Content-Type: application/json
{
"name": "Alice Johnson",
"email": "alice.johnson@example.com",
"department": "Product"
}
Response:
HTTP/1.1 200 OK
Content-Type: application/json
{
"id": 42,
"name": "Alice Johnson",
"email": "alice.johnson@example.com",
"department": "Product",
"updatedAt": "2024-01-16T14:20:00Z"
}
Update - Partial (PATCH)
PATCH /api/users/42 HTTP/1.1
Content-Type: application/json
{
"department": "Design"
}
Response:
HTTP/1.1 200 OK
Content-Type: application/json
{
"id": 42,
"name": "Alice Johnson",
"email": "alice.johnson@example.com",
"department": "Design",
"updatedAt": "2024-01-17T09:15:00Z"
}
Delete (DELETE)
DELETE /api/users/42 HTTP/1.1
Response:
HTTP/1.1 204 No Content
Exercise: Build CRUD Requests
Loading JavaScript Exercise...
PUT vs PATCH: When to Use Which
This is a common source of confusion. Here's how to decide:
Use PUT when:
- You're replacing the entire resource
- The client has the complete, updated representation
- Missing fields should be set to null or defaults
PUT /users/1
{ "name": "Alice", "email": "alice@example.com", "role": "admin" }
// All fields must be provided
Use PATCH when:
- You're updating only specific fields
- You want to leave other fields unchanged
- You're making a small change to a large resource
PATCH /users/1
{ "role": "admin" }
// Only role changes, name and email stay the same
Nested Resource CRUD
For nested resources, CRUD works within the parent context:
POST /users/1/orders # Create order for user 1
GET /users/1/orders # List user 1's orders
GET /users/1/orders/99 # Get specific order
PUT /users/1/orders/99 # Replace order
PATCH /users/1/orders/99 # Update order
DELETE /users/1/orders/99 # Delete order
Summary
| Operation | Method | Collection URI | Resource URI |
|---|---|---|---|
| Create | POST | /items | - |
| List | GET | /items | - |
| Read | GET | - | /items/\{id\} |
| Replace | PUT | - | /items/\{id\} |
| Update | PATCH | - | /items/\{id\} |
| Delete | DELETE | - | /items/\{id\} |
Understanding the CRUD-to-HTTP mapping is essential for designing consistent REST APIs.

