HTTP 方法
GET/POST/PUT/PATCH/DELETE 的语义、安全性与幂等性,以及幂等键的用法。
Key Points方法决定语义:GET 只读、POST 创建、PUT 整体替换、PATCH 局部更新、DELETE 删除;除 POST 外都应幂等。
五个方法
| Method | Meaning | Safe | Idempotent | Typical status |
|---|---|---|---|---|
| GET | Read a resource | Yes | Yes | 200 / 404 |
| POST | Create under a collection | No | No | 201 + Location |
| PUT | Replace the whole resource | No | Yes | 200 / 204 |
| PATCH | Partially update | No | Usually | 200 / 204 |
| DELETE | Remove the resource | No | Yes | 204 / 404 |
安全与幂等
Safe means it does not change server state; idempotent means one call and N calls leave the same result. These properties decide what browsers, proxies and retry logic may do: GET can be prefetched, PUT/DELETE retried automatically, POST cannot.
Text
PUT /users/42 {"name": "Alice"} # 调 3 次,结果一样 —— 幂等
DELETE /users/42 # 第一次 204,之后 404,但状态一致 —— 幂等
POST /users {"name": "Alice"} # 调 3 次建 3 个用户 —— 不幂等PUT 与 PATCH 怎么选
- PUT — the body is the complete new state; omitted fields are cleared
- PATCH — the body carries only the changes; use a diff or JSON Merge Patch
- When in doubt, PATCH for forms that submit a subset of fields
Text
PUT /users/42 {"name": "Alice", "email": "a@x.com", "age": 30}
PATCH /users/42 {"email": "a@x.com"}让 POST 也幂等:幂等键
Retried payments or orders would double-charge. The client sends a unique Idempotency-Key header; the server remembers whether it has seen that key and replays the first result instead of re-executing.
Text
POST /payments HTTP/1.1
Idempotency-Key: 3f2a9c10-8b7e-4a1d-9c2f-1e5b7d0a6c33
Content-Type: application/json
{"orderId": 9001, "amount": 19900}Persist the key (Redis or a unique index) with a TTL such as 24 hours. Keeping it in memory fails as soon as you run more than one instance.
常见误用
- 用 GET 做删除或修改 —— 爬虫和预取会真的删数据
- 用 POST 做查询 —— 失去缓存与可收藏的 URL
- 用 PUT 只传部分字段 —— 没传的字段被清空
- 用 DELETE 返回 200 加一段 JSON —— 用 204 更干净
五个方法
| 方法 | 语义 | 安全 | 幂等 | 典型状态码 |
|---|---|---|---|---|
| GET | 读取资源 | 是 | 是 | 200 / 404 |
| POST | 在集合下创建 | 否 | 否 | 201 + Location |
| PUT | 整体替换资源 | 否 | 是 | 200 / 204 |
| PATCH | 局部更新 | 否 | 通常 | 200 / 204 |
| DELETE | 删除资源 | 否 | 是 | 204 / 404 |
安全与幂等
安全指「不改变服务端状态」,幂等指「执行一次和执行 N 次结果相同」。这两个性质决定了浏览器、代理、重试机制能否放心重发请求:GET 可以被预取,PUT/DELETE 可以被自动重试,POST 不能。
Text
PUT /users/42 {"name": "Alice"} # 调 3 次,结果一样 —— 幂等
DELETE /users/42 # 第一次 204,之后 404,但状态一致 —— 幂等
POST /users {"name": "Alice"} # 调 3 次建 3 个用户 —— 不幂等PUT 与 PATCH 怎么选
- PUT —— body 是完整新状态,没写的字段会被清空
- PATCH —— body 只带改动,用 diff 或 JSON Merge Patch
- 拿不准时,只提交部分字段的表单用 PATCH
Text
PUT /users/42 {"name": "Alice", "email": "a@x.com", "age": 30}
PATCH /users/42 {"email": "a@x.com"}让 POST 也幂等:幂等键
支付、下单这类 POST 一旦被重试就会重复扣款。做法是客户端生成一个唯一键放在 Idempotency-Key 头里,服务端记住「这个键处理过没有」,重复请求直接返回第一次的结果。
Text
POST /payments HTTP/1.1
Idempotency-Key: 3f2a9c10-8b7e-4a1d-9c2f-1e5b7d0a6c33
Content-Type: application/json
{"orderId": 9001, "amount": 19900}幂等键要在服务端落库(Redis 或唯一索引),并设置过期时间(如 24 小时)。只放在内存里,多实例部署时依然会重复扣款。
常见误用
- 用 GET 做删除或修改 —— 爬虫和预取会真的删数据
- 用 POST 做查询 —— 失去缓存与可收藏的 URL
- 用 PUT 只传部分字段 —— 没传的字段被清空
- 用 DELETE 返回 200 加一段 JSON —— 用 204 更干净