CRUD Operations: Mapping HTTP Methods to Data Operations

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

CRUDHTTP MethodURL PatternRequest BodyResponse Code
CreatePOST/resourcesNew resource data201 Created
Read AllGET/resourcesNone200 OK
Read OneGET/resources/:idNone200 OK
Full UpdatePUT/resources/:idComplete resource200 OK
Partial UpdatePATCH/resources/:idChanged fields only200 OK
DeleteDELETE/resources/:idNone204 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

  1. Validates the input data
  2. Generates a unique ID
  3. Sets default values (timestamps, status)
  4. Inserts into the database
  5. Returns the complete resource with its new ID

POST Best Practices

PracticeDescription
Return the created resourceInclude it in the response body
Return 201 CreatedNot 200 OK
Include Location headerURL of the new resource
Validate inputReturn 400 for invalid data
Set defaults server-sideTimestamps, 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

PatternStatusBody
No content204 No ContentEmpty
Confirmation200 OK{"deleted": true}
Return deleted resource200 OKThe 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:

OperationErrorStatusCause
CreateMissing required field400Body missing data
CreateDuplicate entry409Resource already exists
ReadResource not found404Wrong ID or deleted
UpdateResource not found404Wrong ID
UpdateInvalid data422Bad field values
DeleteResource not found404Already deleted
AnyNot authenticated401Missing token
AnyNot authorized403Insufficient role

Summary and Key Takeaways

  1. CRUD maps to HTTP: Create=POST, Read=GET, Update=PUT/PATCH, Delete=DELETE.
  2. POST creates and returns 201; GET reads and returns 200.
  3. PUT replaces entirely; PATCH updates only specified fields.
  4. DELETE removes resources and returns 204 (no content) or 200.
  5. Every CRUD operation has expected error responses you should handle.
  6. 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?

Subscribe to our newsletter

Get the latest posts delivered right to your inbox.

Subscribe on LinkedIn