跳到主要内容
AZ Tools

HTTP 方法参考

真正塑造 API 设计的四个属性 — safe(无服务端副作用)、idempotent(N 次重复相同最终状态)、cacheable(响应可重用)和 body-bearing(请求与响应)— 按方法整理,并附每个方法适用场景的一行简介。

GET

Retrieve a representation of a resource. Should have no side effects.

Safe
幂等
可缓存
Req body
Res body
HEAD

Like GET but with no response body. Used to inspect headers (size, ETag, Last-Modified).

Safe
幂等
可缓存
Req body
Res body
POST

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
PUT

Replace the target resource entirely with the request payload. Calling it twice gives the same result as once.

Safe
幂等
可缓存
Req body
Res body
DELETE

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
PATCH

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
OPTIONS

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
TRACE

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
CONNECT

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)下成立。

使用方法

  1. 输入方法名 (`patch`) 或关键字 (`cache`、`tunnel`) 来过滤。
  2. 阅读每个方法的卡片:顶部为描述,下面是五个属性圆点。
  3. 点复制把方法插入到你的 fetch 调用或 curl 命令中。

常见问题

POST 也能做,为什么 PATCH 有独立方法?
语义。PATCH 意为"应用此部分 diff";POST 为"创建或处理该负载"。如果你的 patch 格式设计得当,调用两次 PATCH 后资源状态与一次相同;这种幂等性保证值得多出一个动词。
POST 真的能缓存吗?
有条件 — RFC 9111 §3 允许在响应携带显式 `Cache-Control` / `Expires` 头时缓存 POST 响应。实际上几乎没人这么做,因此多数缓存把 POST 视为不可缓存。
第二次调用返回 404,DELETE 还算幂等吗?
仍然算。幂等描述的是请求之后服务器的状态,而不是返回的状态码。删一次,资源没了;再删一次,它依然没有——最终状态相同,404 只是在陈述一个已经成立的事实。第一次返回 200、第二次没有任何变化仍返回 200 的 PUT,道理也一样。
搜索条件太长放不进 URL,要改用 POST 吗?
把搜索表达得最贴切的是 GET:安全、可缓存、可分享。但服务器、代理和日志对 URL 长度都有现实上限,放进 URL 的筛选条件还会被沿途每一份访问日志记下来。当条件确实很长或涉及敏感信息时,POST 是务实的选择,代价是失去缓存,也失去一个可以发给同事的链接。

相关工具