HTTP 方法参考
开发
真正塑造 API 设计的四个属性 — safe(无服务端副作用)、idempotent(N 次重复相同最终状态)、cacheable(响应可重用)和 body-bearing(请求与响应)— 按方法整理,并附每个方法适用场景的一行简介。
Retrieve a representation of a resource. Should have no side effects.
- Safe
- 是
- 幂等
- 是
- 可缓存
- 是
- Req body
- 否
- Res body
- 是
Like GET but with no response body. Used to inspect headers (size, ETag, Last-Modified).
- Safe
- 是
- 幂等
- 是
- 可缓存
- 是
- Req body
- 否
- Res body
- 否
Submit data to the server — create a new resource, send a form, or trigger a side-effecting action. Cacheable only when explicit Cache-Control / Expires headers permit.
- Safe
- 否
- 幂等
- 否
- 可缓存
- 条件
- Req body
- 是
- Res body
- 是
Replace the target resource entirely with the request payload. Calling it twice gives the same result as once.
- Safe
- 否
- 幂等
- 是
- 可缓存
- 否
- Req body
- 是
- Res body
- 是
Remove the target resource. Idempotent in the same sense as PUT — deleting an already-gone resource still ends with the same state.
- Safe
- 否
- 幂等
- 是
- 可缓存
- 否
- Req body
- 条件
- Res body
- 是
Apply a partial update to the resource. RFC 5789 leaves the patch format up to the API — JSON Patch (RFC 6902) and JSON Merge Patch (RFC 7396) are the common choices.
- Safe
- 否
- 幂等
- 否
- 可缓存
- 否
- Req body
- 是
- Res body
- 是
Ask what the server supports for the target — methods, CORS preflight, accepted Content-Types. The response usually carries an Allow header.
- Safe
- 是
- 幂等
- 是
- 可缓存
- 否
- Req body
- 否
- Res body
- 是
Echo back the request as the server sees it after proxies. Usually disabled in production for security (Cross-Site Tracing).
- Safe
- 是
- 幂等
- 是
- 可缓存
- 否
- Req body
- 否
- Res body
- 是
Establish a tunnel to the server, used by HTTP proxies for HTTPS. The client and server exchange raw bytes after the proxy accepts.
- Safe
- 否
- 幂等
- 否
- 可缓存
- 否
- Req body
- 否
- Res body
- 否
"条件"表示属性仅在显式头部(POST 的 Cache-Control、DELETE 的 body)下成立。
使用方法
- 输入方法名 (`patch`) 或关键字 (`cache`、`tunnel`) 来过滤。
- 阅读每个方法的卡片:顶部为描述,下面是五个属性圆点。
- 点复制把方法插入到你的 fetch 调用或 curl 命令中。
常见问题
- POST 也能做,为什么 PATCH 有独立方法?
- 语义。PATCH 意为"应用此部分 diff";POST 为"创建或处理该负载"。如果你的 patch 格式设计得当,调用两次 PATCH 后资源状态与一次相同;这种幂等性保证值得多出一个动词。
- POST 真的能缓存吗?
- 有条件 — RFC 9111 §3 允许在响应携带显式 `Cache-Control` / `Expires` 头时缓存 POST 响应。实际上几乎没人这么做,因此多数缓存把 POST 视为不可缓存。