RESTful API Design and HTTP Methods
🎯 Introduction
By the end of this article, you will be able to design resource-oriented endpoints and distinguish reading, creation, replacement, and partial updates.
🧱 Identify the resource before choosing the operation
A resource is a data object exposed and managed by an API, such as a user, article, or file. RESTful APIs commonly use noun-based paths for resources and methods for actions:
| Requirement | Endpoint | Successful result |
|---|---|---|
| List users | GET /users | 200, array |
| Read user details | GET /users/7 | 200, object |
| Create a user | POST /users | 201, new user and address |
| Replace editable profile data | PUT /users/7 | 200 or 204 |
| Update the nickname field | PATCH /users/7 | 200 or 204 |
| Delete a user | DELETE /users/7 | 204 |
Plural nouns are a common convention; consistency within the project matters most. Avoid using /getUser, /users/query, and /userInfo for the same read operation.
📖 GET reads without modifying data
Browsers may preload, cache, or repeat GET requests, so GET must not trigger deletion, deductions, or similar changes. This request should only return user information:
GET /users/7 HTTP/1.1
Host: api.example.com
If simply visiting a link deletes a user, an accidental click or automated request can have side effects. Use an explicit deletion method and check permissions first.
🆕 POST submits data for creation or an operation
To create a user, send:
POST /users HTTP/1.1
Content-Type: application/json
{"name":"Ming"}
After generating the ID, the server returns 201 and Location: /users/7. The server or database should control user IDs; clients must not freely specify internal fields.
Actions that do not fit simple CRUD can also be modeled explicitly. For example, POST /reports/7/exports creates an export record. Do not sacrifice clarity merely to imitate noun-based paths.
🛠 Define field semantics for PUT and PATCH
PUT replaces the target representation according to the contract. Document whether omitted editable fields are cleared. PATCH makes a partial update, usually retaining omitted fields.
PATCH /users/7 HTTP/1.1
Content-Type: application/json
{"name":"New name"}
This PATCH changes only the name; it should not also clear the avatar. null and an absent field may mean different things: null might mean clear, while omission means leave unchanged. Document these semantics.
🔁 Idempotency describes the effect of repetition
Idempotency means repeating an identical request has the same effect on the server's final state as executing it once. Setting a name to “New name” can be idempotent; incrementing a counter by 1 on every call is not.
PUT and DELETE require idempotent semantics, but repeated responses need not be identical. The first deletion may return 204 and the second 404 because the record is missing; the resource remains deleted. POST does not guarantee idempotency by default and needs additional duplicate-submission protection.
Calling a method PUT does not make its implementation correct. A handler that increments a value on every call still violates that contract.
❓ Questions and Answers
Q1: Is PATCH always non-idempotent?
No. Setting a field to a fixed value is usually idempotent; an increment operation may not be. It depends on the patch semantics.
Q2: Must APIs consist strictly of CRUD operations?
No. Define resources and actions clearly. Complex operations can be expressed by creating tasks, for example, rather than using obscure names.
Q3: Why not return the entire object after a successful DELETE?
204 is sufficient when callers only need to know that deletion completed. You can define a 200 response when returning data, but the status must match the actual body.
🧪 Practice
Design endpoints to read, upload or replace, and delete an avatar. Specify the method, path, success status, and expected effect of repeating each request.
// Write your code or record your verification results here
