gpt4 book ai didi

rest - 如果我还更新时间戳属性,我应该使用 PUT 方法进行更新吗

转载 作者:行者123 更新时间:2023-12-03 10:14:45 25 4
gpt4 key购买 nike

更准确地说:

根据rest风格,通常假设POST、GET、PUT和DELETE http方法应该用于CREATE、READ、UPDATE和DELETE(CRUD)操作。

事实上,如果我们坚持 http 方法的定义,事情可能就不那么清楚了

this article它的解释是:

In a nutshell: use PUT if and only if you know both the URL where the resource will live, and the entirety of the contents of the resource. Otherwise, use POST.



主要是因为

PUT is a much more restrictive verb. It takes a complete resource and stores it at the given URL. If there was a resource there previously, it is replaced; if not, a new one is created. These properties support idempotence, which a naive create or update operation might not. I suspect this may be why PUT is defined the way it is; it's an idempotent operation which allows the client to send information to the server.



在我的情况下,我通常发布传递所有资源数据的更新,因此我可以使用 PUT 进行更新,但是每次我发布更新时,我都会保存一个 LastUser 和 LastUpdate 列,其中包含进行修改的用户 ID 和操作时间。

所以我想知道你的意见,因为严格来说这两列不是资源的一部分,但它们确实阻止了操作的幂等性。

问候语

SAS

最佳答案

忽略关于 REST 风格将 CRUD 映射到 HTTP 方法的评论,这是一个很好的问题。

您的问题的答案是,是的,您可以在这种情况下自由使用 PUT,即使服务器以非幂等方式更新了某些资源元素。不幸的是,答案背后的推理相当模糊。重要的是要了解客户端请求的意图。客户端打算用传递的值完全替换资源的内容。客户端不负责服务器执行其他操作,因此不违反 HTTP 方法的语义。

这是用于在您执行 GET 操作时允许服务器更新页面计数器的推理。客户端没有要求更新,因此即使服务器选择进行更新,GET 也是安全的。

完整的资源与部分资源的争论终于在 HTTP spec 的更新中得到了详细说明。

An origin server SHOULD reject any PUT request that contains a Content-Range header field, since it might be misinterpreted as partial content (or might be partial content that is being mistakenly PUT as a full representation). Partial content updates are possible by targeting a separately identified resource with state that overlaps a portion of the larger resource, or by using a different method that has been specifically defined for partial updates (for example, the PATCH method defined in [RFC5789]).



所以,我们现在应该做的事情已经很清楚了。不太清楚的是为什么存在只允许发送完整响应的限制。这个问题已经被问到,恕我直言,在这个关于休息讨论的线程中仍然没有答案。

关于rest - 如果我还更新时间戳属性,我应该使用 PUT 方法进行更新吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5686671/

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