rest-api-design
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseREST API Design
REST API设计
Design RESTful APIs with proper conventions and developer experience.
设计遵循规范的RESTful API,兼顾开发者体验。
Resource Naming
资源命名
undefinedundefinedGood - nouns, plural, hierarchical
推荐写法 - 名词、复数、层级结构
GET /api/users
GET /api/users/123
GET /api/users/123/orders
POST /api/users
PATCH /api/users/123
DELETE /api/users/123
GET /api/users
GET /api/users/123
GET /api/users/123/orders
POST /api/users
PATCH /api/users/123
DELETE /api/users/123
Bad - verbs, actions in URL
不推荐写法 - 动词、URL中包含动作
GET /api/getUsers
POST /api/createUser
POST /api/users/123/delete
undefinedGET /api/getUsers
POST /api/createUser
POST /api/users/123/delete
undefinedHTTP Methods
HTTP方法
| Method | Purpose | Idempotent |
|---|---|---|
| GET | Read resource | Yes |
| POST | Create resource | No |
| PUT | Replace resource | Yes |
| PATCH | Partial update | Yes |
| DELETE | Remove resource | Yes |
| 方法 | 用途 | 幂等性 |
|---|---|---|
| GET | 读取资源 | 是 |
| POST | 创建资源 | 否 |
| PUT | 替换资源 | 是 |
| PATCH | 部分更新 | 是 |
| DELETE | 删除资源 | 是 |
Status Codes
状态码
| Code | Meaning | Use For |
|---|---|---|
| 200 | OK | Successful GET, PATCH |
| 201 | Created | Successful POST |
| 204 | No Content | Successful DELETE |
| 400 | Bad Request | Validation errors |
| 401 | Unauthorized | Missing auth |
| 403 | Forbidden | Insufficient permissions |
| 404 | Not Found | Resource doesn't exist |
| 429 | Too Many Requests | Rate limited |
| 状态码 | 含义 | 适用场景 |
|---|---|---|
| 200 | 请求成功 | 成功的GET、PATCH请求 |
| 201 | 创建成功 | 成功的POST请求 |
| 204 | 无内容 | 成功的DELETE请求 |
| 400 | 错误请求 | 验证失败 |
| 401 | 未授权 | 缺少认证信息 |
| 403 | 禁止访问 | 权限不足 |
| 404 | 未找到 | 资源不存在 |
| 429 | 请求过多 | 触发速率限制 |
Response Format
响应格式
json
{
"data": {
"id": "123",
"type": "user",
"attributes": {
"name": "John",
"email": "john@example.com"
}
},
"meta": {
"requestId": "req_abc123"
}
}json
{
"data": {
"id": "123",
"type": "user",
"attributes": {
"name": "John",
"email": "john@example.com"
}
},
"meta": {
"requestId": "req_abc123"
}
}Collection Response
集合响应
json
{
"data": [...],
"pagination": {
"page": 1,
"limit": 20,
"total": 150,
"totalPages": 8
},
"links": {
"self": "/api/users?page=1",
"next": "/api/users?page=2"
}
}json
{
"data": [...],
"pagination": {
"page": 1,
"limit": 20,
"total": 150,
"totalPages": 8
},
"links": {
"self": "/api/users?page=1",
"next": "/api/users?page=2"
}
}Query Parameters
查询参数
GET /api/products?category=electronics # Filtering
GET /api/products?sort=-price,name # Sorting
GET /api/products?page=2&limit=20 # Pagination
GET /api/products?fields=id,name,price # Field selectionGET /api/products?category=electronics # 过滤
GET /api/products?sort=-price,name # 排序
GET /api/products?page=2&limit=20 # 分页
GET /api/products?fields=id,name,price # 字段选择Best Practices
最佳实践
- Use nouns for resources, not verbs
- Version API via URL path ()
/api/v1/ - Return appropriate status codes
- Include pagination for collections
- Document with OpenAPI/Swagger
- 使用名词表示资源,而非动词
- 通过URL路径为API添加版本标识()
/api/v1/ - 返回合适的状态码
- 为集合类接口添加分页功能
- 使用OpenAPI/Swagger进行文档编写