Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Designing RESTful APIs  ›  Capstone Project

CRUD Operations

CRUD Operations 14 minCreate, read, update and delete map onto POST to the collection and GET, PATCH or PUT, and DELETE on the item, each with a predictable success status.

CRUD Operations

Almost every resource in BookNook needs the same four abilities: add one, look at it, change it, remove it. That is CRUD: create, read, update, delete. Once you map these four onto HTTP the same way everywhere, a developer who has used /books can use /members and /fines without reading anything new.

The rule

Create is POST to the collection; the rest act on the item.

The standard mapping, with the responses a caller should expect:

Operation Request Success response
Create POST /v1/books with the new book 201 Created, Location: /v1/books/43, the new book
Read many GET /v1/books 200 OK with a list
Read one GET /v1/books/43 200 OK with the book, or 404
Update part PATCH /v1/books/43 with changed fields 200 OK with the updated book
Replace PUT /v1/books/43 with the full book 200 OK (or 204)
Delete DELETE /v1/books/43 204 No Content

A create request and its answer:

POST /v1/books HTTP/1.1
Content-Type: application/json

{"title": "Dune", "isbn": "9780441013593", "shelf": "C2"}

HTTP/1.1 201 Created
Location: /v1/books/43
Content-Type: application/json

{"id": 43, "title": "Dune", "isbn": "9780441013593", "shelf": "C2", "available": true}

Notice the server chose the id and filled in available. Returning the full resource saves the caller a second GET.

Details that separate a careful API from a sloppy one:

  • Validate on create and update: missing required fields → 422 with a message saying which field.
  • Read-only fields such as id and created_at are set by the server; ignore or reject attempts to change them.
  • Deleting what is not there: many APIs answer 404; others answer 204 because the end state, "gone", is already true. Pick one and document it.
  • Soft delete: a library may never truly erase a loan, for records. DELETE can mark it removed, as long as it then behaves as gone to callers.

The shortcut

Decide two things for any operation: collection or item? which verb? Only create targets the collection; everything else names the item.

A librarian's day with book 43Step 1 of 4
  1. Add it to the catalogue

    New item, id unknown yet → collection: POST /v1/books. Response 201.

Drills

Answer fast — trust the rule.

quick drill

Which request creates a new member?

quick drill

What is the usual success response to DELETE /v1/books/43?

quick drill

A create request omits the required title. What should BookNook reply?

quick drill

A caller sends PATCH /v1/books/43 with {"id": 99}. What is the best behaviour?

Remember

  • Create is POST to the collection; the rest act on the item.
  • Shortcut: collection or item, then which verb.
  • Trap: missing 201 and Location on create, and letting callers change read-only fields.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainCRUD for fines

Design create, read, update and delete for BookNook's fines resource, with the request and success status for each, and say what happens if a caller tries to change a fine's id.