背景信息分析:

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

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

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

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

我的问题:

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


当前回答

最重要的考虑是可靠性. 如果一个 POST 消息丢失,系统的状态是不确定的. 自动恢复是不可能的. 对于 PUT 消息,状态是不确定的,直到第一次成功退出。

例如,使用 POST 创建信用卡交易可能不是一个好主意。

如果您在您的资源上有自动创建的 URI,您仍然可以使用 PUT 通过向客户端传输一个创建的 URI(指向一个空的资源)。

其他一些考虑:

POST 取消了所有包含资源的隐藏副本(更好的一致性) PUT 回复是不可隐藏的,而 POST 回复是(要求内容位置和终止) PUT 是不受 Java ME、旧浏览器、防火墙等支持。

其他回答

这里是一个简单的规则:

到一个 URL 的 PUT 应该用于更新或创建可以位于该 URL 的资源。

POST to a URL should be used to update or create a resource that is located at some other ("subordinate") URL, or is not locatable via HTTP. URL 应该用于更新或创建位于某个其他(“下级”) URL 或无法通过 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 如发送邮件在邮局。

此分類上一篇

社交媒体/网络分析:

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

添加到上面的所有答案:


在职业生涯中最常用的


我们使用 PUT over POST 在 CREATE 操作中. 为什么? 因为很多在这里也说,答案是不可隐藏的,而 POST 是(要求内容位置和终止)。 我们使用 POST over PUT 在 UPDATE 操作中. 为什么? 因为它破坏了整个包含资源的隐藏副本. 这是有用的,当更新资源。

POST 和 PUT 之间的区别在于服务器如何解释统一资源识别器. 使用 POST, ury 识别服务器上的对象,可以处理所包含的数据. 使用 PUT, 另一方面, ury 识别一个对象,服务器应该放置数据. 虽然 POST ury 通常指示一个程序或脚本, PUT ury 通常是路径和名称 fo

作者建议我们使用PUT上传文件,而不是POST。

正确实施,GET、HEAD、PUT 和 DELETE 方法是无效的,但不是 POST 方法,因此,当你做两个 PUT 时,你会得到一个新的记录,当你做两个 POST 时,你会得到两个新的记录。

但是,请注意,HTML 表单仅支持 GET 和 POST 作为 HTTP 请求方法。

<form method="put"> 是无效的 HTML 并将被处理为, 即发送 GET 请求。

对于我来说,了解区别的关键是了解谁定义资源的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
  |                                 |

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