背景信息分析:

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

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

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

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

我的问题:

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


当前回答

REST 是一个非常高级别的概念,事实上,它甚至没有提到 HTTP!

如果你有任何疑问如何在HTTP中实施REST,你总是可以看看原子出版协议(AtomPub)的规格. AtomPub是由许多HTTP和REST灯台开发的HTTP的RESTful网页服务的标准,其中一些来自Roy Fielding,REST的发明家和(共同)HTTP的发明家。

事实上,你甚至可以直接使用 AtomPub. 虽然它来自博客社区,它没有任何限制博客: 这是一个通用协议,以便通过 HTTP 与自愿的(自愿的)资源集合相互作用。

以下是 AtomPub 對資源創造的說法(第 9.2 節):

要将会员添加到集合中,客户将 POST 请求发送到集合的 URI。

其他回答

创建:

可以用 PUT 或 POST 进行如下:

更新:

只能以以下方式与PUT进行:

PUT 将资源更新为现有资源ID 作为识别器,在 / 资源 URI 或收集下。

例子: <-- 一般 - 具体 -> URI: website.example/users/john website.example - 整个网站用户 - 用户的收集 john - 收集的项目,或资源 URI:website.example/users/john/posts/23 website.example - 整个网站用户 - 用户的收集 john - 收集的项目,或资源帖子 - 收集的帖子 john 23

当你使用POST时,你总是提到收藏,所以每当你说:

POST /users HTTP/1.1

POST /users/john HTTP/1.1

它会工作,但你说你想在用户收藏下添加一个资源。

PUT /users/john HTTP/1.1

让我们来介绍一下特区的一些重要部分:

邮件

因此,它在收藏中创造了一个新的资源。

皮特

参考:

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

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 如发送邮件在邮局。

此分類上一篇

社交媒体/网络分析:

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

对于我来说,了解区别的关键是了解谁定义资源的ID:

例子(与某些地址服务)

POST (sever creates new resource)

client               server/addresses      // NOTE: no ID in the request
  |                                 |
  | --{POST address data}-->        |
  |                                 |
  | <--{201, created addresses/321} |      // NOTE: resource ID in the reply
  |                                 |
PUT (sever sets data of resource, creating it if necessary)

client               server/addresses/321      // NOTE: *you* put the ID here!
  |                                 |
  | --{PUT address data (to 321)}-->|
  |                                 |
  | <--{201, created }              |          // NOTE: resource ID not required here
  |                                 |

这里有很多很好的答案,下面有很好的细节,但这帮助了我到达这个点。

POST 用于将数据发送到服务器. PUT 用于将数据存入服务器上的资源(例如,文件)。

我看到这一点在一个脚本(第55页)从书HTTP: The Definitive Guide。

在恢复已经提到的风险下,似乎很重要的是要记住,PUT意味着客户端在创建资源时会控制什么URL。因此,PUT和POST之间的选择的一部分将是关于您可以信任客户端提供正确的,正常的URL,与您的URL计划是什么相一致。

当您无法完全信任客户端做正确的事情时,更适合使用 POST 创建一个新的项目,然后将 URL 返回客户端在回复中。