背景信息分析:

根据 RFC 2616,第 9.5 条,POST 用于创建资源:

使用 POST 方法要求原始服务器接受请求中包含的实体作为请求-URI 在请求线中识别的资源的新子属。

根据 RFC 2616,第 9.6 条,PUT 用于创建或取代资源:

PUT 方法要求关闭的实体存储在提供的请求-URI. 如果请求-URI 提到已经存在的资源,关闭的实体 SHOULD 将被视为居住在原始服务器的修改版本. 如果请求-URI 没有指向现有资源,并且 URI 能够由请求用户年龄定义为新资源。

我的问题:

那么,哪种HTTP方法应该用于创建资源?或者两者都应该得到支持?


当前回答

两者都用于客户端到服务器之间的数据传输,但它们之间存在微妙的差异,即:

PUT POST
Replacing existing resource or creating if resource is not exist. www.example.com/com/customer/{customerId} www.example.com/com/customer/123/order/{orderId} Identifier is chosen by the client. Creating new resources and subordinate resources, e.g. a file is subordinate to a directory containing it or a row is subordinate to a database table. www.example.com/com/customer/ www.example.com/com/customer/123/order/ identifier is returned by server
Idempotent i.e. if you PUT a resource twice, it has no effect. Example: Do it as many times as you want, the result will be same. x=1; POST is neither safe nor idempotent. Example: x++;
Works as specific Works as abstractive
If you create or update a resource using PUT and then make that same call again, the resource is still there and still has the same state as it did with the first call. Making two identical POST requests will most likely result in two resources containing the same information.

类似:

PUT 即采取并放置它在哪里. POST 如发送邮件在邮局。

此分類上一篇

社交媒体/网络分析:

社交媒体上的帖子:当我们发布消息时,它会创建新的帖子(即编辑)我们已经发布的帖子。

其他回答

Create => HTTP PUT
Retrieve => HTTP GET
Update => HTTP POST
Delete => HTTP DELETE

事实上, CRUD 操作的 R(选择) 和 D(选择) 可以直接地图到 HTTP 方法 GET 和 DELETE 相应。 然而,混淆在 C(现实) 和 U(更新) 操作中。 在某些情况下,可以使用 PUT 为创建而在其他情况下需要 POST。

基于上述定义,我的使用 HTTP PUT 方法与使用 HTTP POST 方法为 REST 服务是: 使用 HTTP PUT 方法,当:

The client includes all aspect of the resource including the unique identifier to uniquely identify the resource. Example: creating a new employee.
The client provides all the information for a resource to be able to modify that resource.This implies that the server side does not update any aspect of the resource (such as an update date).

The server will provide some information concerning the newly created resource. For example, take a logging system. A new entry in the log will most likely have a numbering scheme which is determined on the server side. Upon creating a new log entry, the new sequence number will be determined by the server and not by the client.
On a modification of a resource, the server will provide such information as a resource state or an update date. Again in this case not all information was provided by the client and the resource will be changing from one modification request to the next. Hence a non idempotent operation.

结论

上面和下面的所有答案都是正确的,只是一个小(重要)笔记. 所有这些“句子”都是建议,他们的效果没有执行. 服务器是免费的做他们想要什么,这意味着写与GET或服务器想要什么。

PHP 例如,阅读 $_POST 和 $_GET. 它完全取决于程序员将通过从这些序列中阅读变量进行什么。

大多数时候,你会用它们如下:

POST a resource into a collection PUT a resource identified by collection/:id。

例如:

POST /items PUT /items/1234

请注意,POST在收藏中“创建”一个新的元素,而PUT在某个URL中“替换”一个元素,但使用PUT进行部分修改是一种非常常见的做法,也就是说,只使用它来更新现有资源,并仅修改体内所包含的字段(忽略其他字段)。

请记住,REST是一组条约和指导方针,以保持您的API简单。 如果您结束了一个复杂的工作周围,只需检查“RESTfull”框,那么您正在击败目的。

Ruby on Rails 4.0 将使用“PATCH”方法而不是 PUT 进行部分更新。

RFC 5789 讲述了 PATCH (自 1995 年以来):

一个新的方法是必要的,以改善互动性和防止错误. PUT 方法已经定义,以重新编写一个资源与一个完整的新体,并且不能重新使用,以进行部分变更。 否则, proxies 和 cache,甚至客户端和服务器,可能会被混淆的结果操作。 POST 已经使用,但没有广泛的互动性(

“Edge Rails: PATCH 是更新的新主要 HTTP 方法,”它解释。

原始服务器可以使用此 URI 创建资源

所以你使用POST和可能,但不需要PUT为资源创建. 你不需要支持两者. 对于我来说,POST是完美的足够。

如您的引用所提到的,您使用 PUT 创建没有资源分配到 IRI,您希望创建一个资源无论如何. 例如, PUT /users/123/password 通常将旧密码替换为新密码,但您可以使用它创建一个密码,如果它已经不存在(例如,由新鲜注册的用户或通过恢复被禁止的用户)。