RESTful API Cheatsheet - REST API Design Reference

Inconsistent endpoint naming is what turns a healthy API into a guessing game for every frontend engineer. Organized by URL design, methods, status codes, headers/formats, auth and practices, each entry is a convention you can apply directly. Use it to sketch a resource model, choose /users/{id} over /getUser, and align on error framing. After reading you produce an API whose shape your frontend bets on safely.

Reference·46 commands·Last updated 2026-07-21

URL Design Spec 6

/api/v1/users
资源set(复数名词)
/api/v1/users/123
单个资源(ID 定位)
/api/v1/users/123/orders
子资源嵌套
/api/v1/users?role=admin
查询OptionsFilter
/api/v1/users?page=2&limit=20
分页Options
/api/v1/users?sort=-created_at
sort(- 表示descending)

HTTP Methods 8

GET /users
Get资源list(安全、幂)
GET /users/123
Get单个资源
POST /users
Create资源(非幂)
PUT /users/123
完整Update资源(幂)
PATCH /users/123
部分Update资源
DELETE /users/123
Delete资源(幂)
HEAD /users/123
只Getresponse头,不返回 body
OPTIONS /users
查询supports的 HTTP method(CORS 预检)

Common Status Codes 12

200 OK
request成功
201 Created
资源Create成功
204 No Content
成功但无返回内容(如 DELETE)
301 Moved Permanently
资源永久redirect
400 Bad Request
requestOptions错误or格式不合法
401 Unauthorized
未认证(缺少or无效凭证)
403 Forbidden
已认证但无Permission访问
404 Not Found
资源不exists
409 Conflict
资源冲突(如重复Create)
422 Unprocessable Entity
语义错误(field校验失败)
429 Too Many Requests
request频率超限(触发限流)
500 Internal Server Error
Service器内部错误

Request Headers 5

Content-Type: application/json
request体为 JSON 格式
Accept: application/json
期望response为 JSON
Authorization: Bearer <token>
Bearer Token 认证头
If-None-Match: "etag"
conditionrequest(ETag cache校验)
X-API-Key: abc123
自定义 API Key 认证

Response Format 5

{ "data": [...], "meta": {...} }
标准listresponse结构
{ "data": {...} }
单资源response结构
{ "error": { "code": "...", "message": "..." } }
错误response结构
{ "page": 2, "limit": 20, "total": 100 }
分页元信息
Link: <url>; rel="next"
分页链接头(RFC 5988)

Auth Methods 5

Authorization: Basic base64(user:pass)
HTTP 基本认证
Authorization: Bearer <jwt>
Bearer Token / JWT 认证
X-API-Key: <key>
API Key 认证(自定义头)
?api_key=<key>
API Key 认证(查询Options)
OAuth 2.0
授权码模式(authorization code flow)

Best Practices 5

幂等性设计
GET/PUT/DELETE 应幂,多次call结果一致
POST 创建返回 201 + Location
Create成功返回 201 和资源 URI
批量操作: POST /users/batch
batchCreate/Update使用子资源端点
RateLimit-Limit / Retry-After
限流response头告知客户端配额
HATEOAS
response中package含相关资源链接(超媒体驱动)

Tips

  • Use plural nouns for resource collections in URLs, e.g. /users not /user.
  • GET should be safe (no server state change); PUT/DELETE should be idempotent.
  • PUT fully replaces; PATCH partially updates — choose by scenario.
  • Put the API version in the URL path (/v1/) or a request header (Accept-Version).
  • Error responses should include a code, message, and optional details for easy client handling.

Official References

Each command links to its official documentation below, so you can verify the latest usage and read deeper.

Maintained by LaoHand

Publicly updated on Jul 21, 2026, continuously proofread against official docs.

Contact Us

Wrong command or description? Send us corrections, business inquiries or product feedback by email.

Contact Us