[英]REST - Shouldn't PUT = Create and POST = Update
Shouldn't PUT be used to Create
and POST used to Update
since PUT is idempotent.不应将PUT用于
Create
和POST用于Update
因为 PUT 是幂等的。
That way multiple PUTs for the same Order will place only one Order?这样同一订单的多个 PUT 只会下一个订单?
The difference is that a PUT is for a known resource, and therefor used for updating, as stated here in rfc2616.不同之处在于 PUT 用于已知资源,因此用于更新,如rfc2616 中所述。
The fundamental difference between the POST and PUT requests is reflected in the different meaning of the Request-URI.
POST 和 PUT 请求的根本区别体现在 Request-URI 的不同含义上。 The URI in a POST request identifies the resource that will handle the enclosed entity.
POST 请求中的 URI 标识将处理封闭实体的资源。 That resource might be a data-accepting process, a gateway to some other protocol, or a separate entity that accepts annotations.
该资源可能是一个数据接受进程、一个通往其他协议的网关,或者是一个接受注解的独立实体。 In contrast, the URI in a PUT request identifies the entity enclosed with the request -- the user agent knows what URI is intended and the server MUST NOT attempt to apply the request to some other resource.
相比之下,PUT 请求中的 URI 标识了包含在请求中的实体——用户代理知道 URI 的意图,并且服务器不得尝试将请求应用于某些其他资源。
I do see where you are coming from based on the names themselves however.但是,我确实根据名称本身了解您来自哪里。
I usually look at POST as it should be the URI that will handle the content of my request (in most cases the params as form values) and thus creating a new resource, and PUT as the URI which is the subject of my request (/users/1234), a resource which already exists.我通常会查看 POST,因为它应该是处理我的请求内容的 URI(在大多数情况下,参数作为表单值)并因此创建一个新资源,并将 PUT 作为 URI,它是我的请求的主题(/ users/1234),一个已经存在的资源。
I believe the nomenclature goes back a long ways, consider the early web.我相信命名法可以追溯到很长的路要走,考虑一下早期的网络。 One might want to
POST
their message to a message board, and then PUT
additional content into their message at a later date.有人可能会想
POST
他们的消息,一个留言板,然后PUT
在以后的附加内容到他们的消息。
There's no strict correspondence between HTTP methods and CRUD. HTTP 方法和 CRUD 之间没有严格的对应关系。 This is a convention adopted by some frameworks, but it has nothing to do with REST constraints.
这是一些框架采用的约定,但与 REST 约束无关。
A PUT
request asks the server to replace whatever is at the given URI with the enclosed representation, completely ignoring the current contents. PUT
请求要求服务器用包含的表示替换给定 URI 中的任何内容,完全忽略当前内容。 A good analogy is the mv
command in a shell.一个很好的类比是 shell 中的
mv
命令。 It creates the new file at the destination if it doesn't exist, or replaces whatever exists.如果新文件不存在,它会在目的地创建新文件,或者替换任何存在的文件。 In either case, it completely ignores whatever is in there.
无论哪种情况,它都会完全忽略其中的任何内容。 You can use this to create, but also to update something, as long as you're sending a complete representation.
只要您发送完整的表示,您就可以使用它来创建和更新某些内容。
POST
asks the target resource to process the payload according to predefined rules, so it's the method to use for any operation that isn't already standardized by the HTTP protocol. POST
要求目标资源根据预定义的规则处理负载,因此它是用于 HTTP 协议尚未标准化的任何操作的方法。 This means a POST
can do anything you want, as long as you're not duplicating functionality from other method -- for instance, using POST
for retrieval when you should be using GET
-- and you document it properly.这意味着
POST
可以做任何您想做的事情,只要您不复制其他方法的功能——例如,当您应该使用GET
时使用POST
进行检索——并且您正确记录了它。
So, you can use both for create and update, depending on the exact circumstances, but with PUT
you must have consistent semantics for everything in your API and you can't make partial updates, and with POST
you can do anything you want, as long as you document how exactly it works.因此,您可以根据具体情况同时使用创建和更新,但是使用
PUT
您必须对 API 中的所有内容具有一致的语义,并且您不能进行部分更新,使用POST
您可以做任何您想做的事情,如只要你记录它是如何工作的。
PUT should be used for creates if and only if possible URI of the new resource is known for a client.当且仅当客户端知道新资源的可能 URI 时,才应使用 PUT 来创建。 New URI maybe advertised by the service in resource representation.
新 URI 可能由服务在资源表示中公布。 For example service may provide with some kind of submit form and specify action URI on it which can be a pre populated URI of the new resource.
例如,服务可能会提供某种提交表单并在其上指定操作 URI,该 URI 可以是新资源的预填充 URI。 In this case yes, if initial PUT request successfully creates resource following PUT request will only replace it.
在这种情况下是的,如果初始 PUT 请求成功创建资源,则 PUT 请求将仅替换它。
It's ok to use POST for updates, it was never said that POST is for "create" operations only.可以使用 POST 进行更新,从来没有说 POST 仅用于“创建”操作。
You are trying to correlate CRUD to HTTP, and that doesn't work.您试图将 CRUD 与 HTTP 相关联,但这是行不通的。 The philosophy of HTTP is different, and does not natively correspond to CRUD.
HTTP的理念是不同的,并且本身不对应于CRUD。 The confusion arises because of REST;
混淆是因为 REST; which does correspond to CRUD.
这确实对应于 CRUD。 REST uses HTTP, but with additional constraints upon what is allowed.
REST 使用 HTTP,但对允许的内容有额外的限制。 I've prepared this Q & A to explain the HTTP approach to things:
我准备了这个问答来解释 HTTP 方法:
What's being requested?要求什么?
POST
requests an action upon a collection . POST
请求对集合执行操作。PUT
requests the placement of a resource into a collection. PUT
请求将资源放置到集合中。 What kind of object is named in the URI? URI 中命名了什么样的对象?
POST
identifies a collection . POST
的 URI 标识一个集合。PUT
identifies a resource (within a collection). PUT
的 URI 标识资源(在集合内)。 How is the object specified in the URI, for POST
and PUT
respectively? URI 中的对象是如何分别为
POST
和PUT
指定的?
/collectionId
/collectionId/resourceId
How much freedom does the HTTP protocol grant the collection? HTTP 协议赋予集合多大的自由度?
POST
, the collection is in control.POST
,集合处于控制之中。PUT
, the requestor is in control (unless request fails).PUT
,请求者处于控制之中(除非请求失败)。 What guarantees does the HTTP protocol make? HTTP 协议做了什么保证?
POST
, the HTTP protocol does not define what is supposed to happen with the collection;POST
,HTTP 协议没有定义集合应该发生什么; the rfc states that the server should " process ... the request according to the [collection's] own specific semantics. " (FYI: The rfc uses the confusing phrase "target resource" to mean "collection".) It is up to the server to decide upon a contract that defines what a POST
will do. POST
将做什么的合同。PUT
, the HTTP protocol requires that a response of "success" must guarantee that the collection now contains a resource with the ID and content specified by the request.PUT
,HTTP 协议要求“成功”响应必须保证集合现在包含具有请求指定的 ID 和内容的资源。 Can the operation result in the creation of a new resource within the collection?该操作是否会导致在集合中创建新资源?
POST
creates a new resource, the response will be 201.POST
创建新资源时,响应将为 201。PUT
will generally not insert, but only update.) When a PUT
creates a new resource, the response will be 201. PUT
通常不会插入,而只会更新。)当PUT
创建新资源时,响应将为 201。 Is the operation idempotent?操作是幂等的吗?
POST
is generally not idempotent. POST
通常不是幂等的。 (The server can offer any contract it wishes, but idempotency is generally not part of that contract). PUT
is required to be idempotent. PUT
必须是幂等的。 (The state of the identified resource is idempotent. Side effects outside of that resource are allowed.) Here is the rfc: https://tools.ietf.org/html/rfc7231#section-4.3.3这是 rfc: https : //tools.ietf.org/html/rfc7231#section-4.3.3
It depends.. you can create/update sites/records with both.这取决于……您可以同时创建/更新站点/记录。 When the client is specifying the URI then PUT is the way to go.
当客户端指定 URI 时,PUT 是要走的路。 eg Any Code Editor like Dreamweaver, PUT is the right protocol to use.
例如,任何像 Dreamweaver 这样的代码编辑器,PUT 都是使用的正确协议。
have also a look at this thread: put vs post in rest也看看这个线程: put vs post in rest
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.