背景信息分析:

根据 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.

结论

您可以在网页上找到声明说

也不是很正确。


最好选择 PUT 和 POST 之间,基于行动的可行性。

PUT 意味着放置一个资源 - 完全取代在特定 URL 上可用的任何东西与不同的东西. 根据定义,一个 PUT 是无能为力. 尽量尽量多次,结果是相同的. x=5 是无能为力. 你可以 PUT 一个资源,无论它以前存在,还是不存在(例如,创建,或更新)!

POST 更新一个资源,添加一个子资源,或引发一个变化. 一个 POST 不是 idempotent,因此 x++ 不是 idempotent。


根据这个论点,PUT是为了创建,当你知道你将创建的东西的URL。POST可以用来创建,当你知道“工厂”的URL或管理者为你想要创建的东西类别。

如:

POST /expense-report

或:

PUT  /expense-report/10929

总体:

PUT 和 POST 都可以用于创建。

你必须问“你在做什么?”来区分你应该使用什么,假设你正在设计一个API来提出问题,如果你想使用POST,那么你会这样做一个问题列表,如果你想使用PUT,那么你会这样做一个特定的问题。

你不需要支持PUT和POST。

一些考虑:

我写了以下,作为关于此问题的另一个答案的一部分:

POST: 用于修改和更新资源 POST / 问题/<existing_question> HTTP/1.1 主机: www.example.com/ 注意下列是错误: POST / 问题/<new_question> HTTP/1.1 主机: www.example.com/ 如果 URL 尚未创建,您不应该使用 POST 创建它,同时指定名称。

邮件 vs 邮件

邮件

http://example.com/form

http://example.com/form/568

PUT需要知道这个地址点给一个实体!

http://example.com/097243523581549126143698

您是否知道过去 20+ 年内浏览器中的 HTML 表格方法属性仅支持 POST 和 GET,并且不支持 PUT?

POST and GET work ok...
<form id="form1" name="form1" method="get" action="/form">...</form>
<form id="form2" name="form2" method="post" action="/form">...</form>

PUT NOT SUPPORTED!
<form id="form3" name="form3" method="put" action="/form">...</form>

DELETE NOT SUPPORTED!
<form id="form4" name="form4" method="delete" action="/form">...</form>

疯了啊?

然后,服务器必须仔细观察这个URL(加上按下“添加”,“删除”,“更新”等任何按钮)决定什么是正确的字母,然后路由POST或PUT或DELETE等到正确的服务器处理器。

事实上,PUT一直连接到URL识别器,而不是您的客户端侧代码的逻辑,但它是当今客户端侧模型的错误性质,其破碎的HTML5设计,加上当今厚客户端JavaScript API呼叫和其他循环技巧的腐败性质,这些技巧污染了应该是一个非常简单的HTTP标准,很久以前由非常有前途的人创建。

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

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