知识卡片

普通读书笔记卡 · 3437.a.1

服务端选URI时用POST

理解等级:中级

创建新资源时,如果由服务端决定它的 URI,书中依所引 HTTP 规范建议客户端向集合路径发送 POST。PUT 可用于已知目标 URI 的更新,也具备幂等语义;不能把“PUT 永远不能创建资源”当作通用规则。

这张卡片看不懂?

已有反馈 · 0 / 10 条

    还没有人提交反馈。

    原书出处

    参考来源

    - 位置:《Microservice APIs》第 4 章《Principles of REST API design》(源文件:_epub-src/OEBPS/Text/04.htm) - 结论依据:原文先承认 PUT 可幂等并可创建不存在的资源,再引用规范指出服务端为客户端选择 URI 时宜采用 POST;卡片保留该条件,未沿用原文随后过度绝对的说法。 - 原始内容:According to the HTTP specification, PUT can be idempotent, and therefore we can use it to create a resource if it doesn’t exist. However, the specification also highlights “[a] service that selects a proper URI on behalf of the client, after receiving a state-changing request, SHOULD be implemented using the POST method rather than PUT.”