如果我还更新了timestamp属性,是否应该使用PUT方法进行更新 [英] should I use PUT method for update, if I also update a timestamp attribute
问题描述
更准确地说:
根据其余样式,通常假定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.