Mapping CRUD to methods and status codes
40 min · Intermediate
Need help? Ask a tutor on WhatsApp (opens in a new tab)A mobile app saves a new product and shows "Done", although the server rejected it: the API answered 200 to everything. Clients need fixed method and status pairs to know what really happened.
List and read: GET with 200 or 404
CRUD means Create, Read, Update, Delete: the four operations most resources need. A REST API maps each one to a standard HTTP method and a standard success code.
Read product 3, which exists, and product 99, which does not.
Example 1 · GET an item: found and not found
GET /api/v1/products/3 HTTP/1.1Host: localhost:8080GET /api/v1/products/99 HTTP/1.1Host: localhost:8080HTTP/1.1 200 OK
Content-Type: application/json
{
"id": 3,
"sku": "MOB-IP15",
"name": "iPhone 15 128GB",
"categoryId": 2,
"categoryName": "Mobiles",
"price": 69900.00,
"stock": 8,
"isActive": true
}
HTTP/1.1 404 Not Found
Content-Type: application/problem+json
{
"type": "https://shoplite.example/problems/not-found",
"title": "Resource not found",
"status": 404,
"detail": "Product 99 was not found.",
"instance": "/api/v1/products/99"
}What this code does
- Product 3 exists, so the answer is 200 with its JSON. Product 99 does not, so the answer is 404.
- The 404 body uses the ShopLite error format (
application/problem+json), explained fully in the Error response format lesson. A client can branch on the status code alone.
GET reads a resource without changing anything. On success the server answers 200 OK with the representation in the body: an array or page for a collection, one object for an item. When the item in the URL does not exist, the answer is 404 Not Found, never 200 with an empty body or null, so a client that gets 200 can trust the body.
An empty collection is not an error: GET /customers/8/orders for a customer with no orders is 200 with an empty list. 404 means the URL itself names nothing.
Create: POST to the collection, 201 Created
The Location header holds the URL of a newly created resource. A 201 Created response carries it, with the created item in the body.
Create a category by sending POST to the collection.
Example 2 · POST creates a category: 201 with Location
POST /api/v1/categories HTTP/1.1Host: localhost:8080Content-Type: application/json{ "name": "Stationery", "parentId": null}HTTP/1.1 201 Created
Location: http://localhost:8080/api/v1/categories/8
Content-Type: application/json
{
"id": 8,
"name": "Stationery",
"parentId": null
}What this code does
- The request goes to the collection
/categoriesand has no id: the server chooses it (8, the next number in a fresh seed database). Locationgives the full URL of the new item, and the body repeats the saved category with its id, so the client can show it at once.
POST to a collection URL creates a new member, and the server chooses its id. Success is 201 Created. The body matters because the server adds things the client did not send: the id, default values (isActive: true) and computed fields (categoryName). Without it the client would need a second GET just to show the new row.
Update: PUT replaces, PATCH changes some fields
An operation is idempotent when sending it twice leaves the same state as sending it once. GET, PUT and DELETE are idempotent; POST is not.
PUT replaces every field of product 6; PATCH changes only its price.
Example 3 · PUT replaces every field of product 6
PUT /api/v1/products/6 HTTP/1.1Host: localhost:8080Content-Type: application/json{ "sku": "ACC-AD141", "name": "boAt Airdopes 141 ANC", "categoryId": 4, "price": 1499.00, "stock": 110, "isActive": true}HTTP/1.1 200 OK
Content-Type: application/json
{
"id": 6,
"sku": "ACC-AD141",
"name": "boAt Airdopes 141 ANC",
"categoryId": 4,
"categoryName": "Accessories",
"price": 1499.00,
"stock": 110,
"isActive": true
}What this code does
- PUT sends all six editable fields. Leaving out
stockwould be a 400 error, not "keep the old stock". - The answer is 200 with the full item, including
categoryName, which the server fills in from the category.
Example 4 · PATCH changes only the price
PATCH /api/v1/products/6 HTTP/1.1Host: localhost:8080Content-Type: application/json{ "price": 1199.00}HTTP/1.1 200 OK
Content-Type: application/json
{
"id": 6,
"sku": "ACC-AD141",
"name": "boAt Airdopes 141",
"categoryId": 4,
"categoryName": "Accessories",
"price": 1199.00,
"stock": 120,
"isActive": true
}What this code does
- Only
priceis sent; every other field keeps its value. A PATCH with an empty body{}is a 400 error in ShopLite, because it changes nothing. - Use PATCH for small edits from a screen (a price, a stock count) and PUT when the client has the whole object.
PUT sends the full new state of an item; every editable field is required, and a missing field is an error, not "leave it alone". PATCH sends only the fields to change. Both go to the item URL.
After a successful update the server may answer 200 OK with the updated item, or 204 No Content with no body. ShopLite always answers 200 with the item, so the client sees the result of server-side rules without another request. Because PUT is idempotent, it is safe to retry after a network timeout.
Delete: 204, and the second DELETE
204 No Content is the success status for a request that returns no body. ShopLite answers a successful DELETE with it.
Delete product 16 twice.
Example 5 · DELETE twice: 204, then 404
DELETE /api/v1/products/16 HTTP/1.1Host: localhost:8080DELETE /api/v1/products/16 HTTP/1.1Host: localhost:8080HTTP/1.1 204 No Content
HTTP/1.1 404 Not Found
Content-Type: application/problem+json
{
"type": "https://shoplite.example/problems/not-found",
"title": "Resource not found",
"status": 404,
"detail": "Product 16 was not found.",
"instance": "/api/v1/products/16"
}What this code does
- Product 16 (a new keyboard that was never ordered) is removed: 204 with no body. The second call finds nothing and gets 404.
- The database is the same after one call or two, which is what idempotent means. Deleting product 1, which appears in orders, is refused with 409; the error format lesson shows it.
DELETE on an item URL removes it. There is nothing left to show, so the answer has no body.
DELETE is idempotent: the state is the same after one call or two, but the response can differ. ShopLite answers the second DELETE with 404, which tells the client honestly that nothing is there. Some APIs answer 204 again; both are allowed, so the contract must say which.
Errors on writes: 400, 409 and 422
409 Conflict means the request is valid but clashes with the current state of the data, such as a duplicate SKU, not enough stock or a shipped order that cannot be cancelled.
One create request, sent three times with a different mistake each time.
Example 6 · One create, three kinds of failure
POST /api/v1/products HTTP/1.1Host: localhost:8080Content-Type: application/json{ "sku": "ACC-KB100", "name": "Zebronics Wireless Keyboard", "categoryId": 4, "price": "§-5.00§", "stock": 30}POST /api/v1/products HTTP/1.1Host: localhost:8080Content-Type: application/json{ "sku": "ACC-KB100", "name": "Zebronics Wireless Keyboard", "categoryId": 42, "price": 999.00, "stock": 30}POST /api/v1/products HTTP/1.1Host: localhost:8080Content-Type: application/json{ "sku": "MOB-RN13", "name": "Zebronics Wireless Keyboard", "categoryId": 4, "price": 999.00, "stock": 30}HTTP/1.1 400 Bad Request
Content-Type: application/problem+json
{
"type": "https://shoplite.example/problems/validation-failed",
"title": "Validation failed",
"status": 400,
"detail": "The request has 1 invalid field.",
"instance": "/api/v1/products",
"errors": [
{ "field": "price", "message": "must be greater than 0" }
]
}
HTTP/1.1 422 Unprocessable Content
Content-Type: application/problem+json
{
"type": "https://shoplite.example/problems/rule-broken",
"title": "Business rule broken",
"status": 422,
"detail": "Category 42 does not exist.",
"instance": "/api/v1/products"
}
HTTP/1.1 409 Conflict
Content-Type: application/problem+json
{
"type": "https://shoplite.example/problems/already-exists",
"title": "Resource already exists",
"status": 409,
"detail": "A product with SKU MOB-RN13 already exists.",
"instance": "/api/v1/products"
}What this code does
- A negative price is wrong in the request itself: 400. Category 42 is a well-formed number that points at nothing: 422.
- SKU
MOB-RN13is valid but already used by the Redmi phone: a clash with stored data, so 409. Each code tells the client a different next step.
A failed create or update needs the right 4xx code (a client-side error), so the client knows whether to fix the request, refresh its data or show a message. ShopLite uses three codes for failed writes:
- 400 Bad Request: the request itself is wrong: not JSON, a missing field, a negative price. Fix the request.
- 409 Conflict: valid, but it clashes with the current state: a duplicate SKU, not enough stock, cancelling a shipped order.
- 422 Unprocessable Content: valid field by field, but it breaks a rule involving other data:
categoryId42 does not exist. Older documents call it "Unprocessable Entity"; it is the same code.
The server checks in a fixed order, and the first failed check decides the code. First the body and its fields (400), then the URL's item (404), then references to other data (422), and last any clash with stored data (409).
Predict the output
Which status does ShopLite answer?
Each request fails. Pick the status code.
Round 1 of 4
Customer Asha Rao already exists with this e-mail.
Next step
Check your understanding
12 quick questions