gpt4 book ai didi

web-services - RESTful 创建或更新的正确 HTTP 方法?

转载 作者:行者123 更新时间:2023-12-03 13:29:03 24 4
gpt4 key购买 nike

我正在创建一个 RESTful Web 服务,它允许我按名称导入文档。我会使用这样的路径导入文档:

/documents/frequently-asked-questions

如果文档不存在,它会创建一个新的;否则,它只会覆盖现有文档。

我的问题是这是否是 RESTful 服务的错​​误端点。通常我会使用 POST 进行创建,使用 PUT 进行更新。这里事先不知道该文件是否已经存在。如果这是合理的,那么最好的 HTTP 方法是什么?如果它不正确,那么更好的方法是什么?

最佳答案

HTTP 1.1 规范对 POST 说:

9.5 POST

The POST method is used to request that the origin server accept the entity enclosed in the request as a new subordinate of the resource identified by the Request-URI in the Request-Line.


对于 PUT:

9.6 PUT

The PUT method requests that the enclosed entity be stored under the supplied Request-URI.


鉴于此,并且 PUT 是幂等的而 POST 不是的事实,PUT 在这里似乎是您创建和更新的合乎逻辑的选择。
来源:
http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.1.2

关于web-services - RESTful 创建或更新的正确 HTTP 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7297578/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com