如果我还更新了timestamp属性,是否应该使用PUT方法进行更新 [英] should I use PUT method for update, if I also update a timestamp attribute

查看:236
本文介绍了如果我还更新了timestamp属性,是否应该使用PUT方法进行更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更准确地说:

根据其余样式,通常假定POST,GET,PUT和DELETE http方法应用于CREATE,READ,UPDATE和DELETE(CRUD)操作.

According to rest style, it's generally assummed that POST, GET, PUT, and DELETE http methods should be used for CREATE, READ, UPDATE and DELETE (CRUD) operations.

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

In fact, if we stick to the http methods definition the thing might not be so clear

本文中,其解释是:

简而言之:只有且仅当您知道资源将驻留的URL以及资源的全部内容时,才使用PUT.否则,请使用POST.

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是限制性更强的动词.它需要完整的资源并将其存储在给定的URL中.如果以前有资源,则将其替换;如果不是,则创建一个新的.这些属性支持幂等,而天真的创建或更新操作可能不支持幂等.我怀疑这可能就是为什么按这样定义PUT的原因.这是一个幂等操作,它允许客户端将信息发送到服务器.

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和时间od操作.

In my case I usually issue updates passing all the resource data, so I could use PUT for updates, but everytime I issue an update I save a LastUser and LastUpdate column, with the user id that made the modification and the time od the operation.

因此,我想听听您的意见,因为严格来讲,这两列不是资源的一部分,但它们确实阻止了操作成为幂等.

So i'd like to know your opinion, because strictly speaking those two columns are not part of the resource, but they do prevent the operation from being idempotent.

saludos

sas

推荐答案

忽略有关将CRUD映射到HTTP方法的REST样式的注释,这是一个很好的问题.

Ignoring the comment about the REST style mapping CRUD to the HTTP methods, this is an excellent question.

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

The answer to your question is, yes you are free to use PUT in this scenario even though there are some elements of the resource that are updated by the server in a non-idempotent manner. Unfortunately, the reasoning behind the answer is quite vague. The important thing, is to understand what was the intent of the client request. The client intended to completely replace the contents of resource with the values passed. The client is not responsible for the server doing other operations and therefore the semantics of the HTTP method are not violated.

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

This is the reasoning that is used to allow a server to update a page counter when you do a GET operation. The client didn't ask for an update therefore the GET is safe even though the server chose to make an update.

在更新

原始服务器应拒绝任何PUT 要求包含 内容范围标头字段,因为它 可能会被误解为部分 内容(或可能是部分内容) 被误认为是 完整的代表).部分内容 通过定位一个 分别确定的资源 重叠一部分的状态 较大的资源,或使用 已经有不同的方法 专为部分定义 更新(例如PATCH [RFC5789]中定义的方法).

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]).

因此,我们应该做的已经很清楚了.尚不清楚的是为什么存在只允许发送完整响应的限制.有人问过这个问题,恕我直言,关于静息讨论的话题仍未得到解答.

So, what we are supposed to do is now clear. What is not so clear is why there exists this constraint on only being allowed to send full responses. That question has been asked and IMHO remains unanswered in this thread on rest-discuss.

这篇关于如果我还更新了timestamp属性,是否应该使用PUT方法进行更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆