背景信息分析:

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

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

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

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

我的问题:

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


当前回答

您应该使用 PATCH. 您应该使用 PATCH 等问题列表

PATCH /questions HTTP/1.1

包含您要创建的对象的列表,如

[
    {
        "title": "I said semantics!",
        "content": "Is this serious?",
        "answer": "Not really"
    }
]

您将修改现有资源列表,而无需提供全新的内容,您将从不存在的数据转换为现有信息的状态,而无需提供所有数据(服务器最有可能将添加身份证)。

这个方法的一个很好的优点是,您可以使用单个请求创建多个实体,简单地将所有实体列入列表中。

一个缺点是,可能没有人这样使用PATCH,我害怕,我只是发明了它,但我希望,我提供了一个很好的论点。

至于语法,CREATE是IMHO的唯一正确选择,其余的一切都是一个圆洞的平方点,不幸的是,我们所有的都是圆洞。

其他回答

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”,如“GET”被认为是无效的 - 也就是说,你可以要求相同的准确的PUT多次,结果将是像你执行它只有一次。

我将描述我认为最广泛使用和最有用的条约:

当你在一个特定的URL中找到一个资源时,发生的事情就是它应该在这个URL上存储,或者沿着这些行走。

例如,当你想创建一个新流时,你可以将其输入到某些URL,但是当你想向现有流发送一个消息时,你可以将其输入到URL。

至于修改流的属性,您可以使用 PUT 或 POST. 基本上,只使用“PUT”当操作是无效的 - 否则使用 POST。

但是,请注意,并不是所有现代浏览器都支持其他HTTP字母,而不是GET或POST。

这里是一个简单的规则:

到一个 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 找到的资源。


POST 可以执行其他三项功能,但随后请求的语法将失去在中间人,如Caches 和 Proxy. 这也适用于提供资源的安全性,因为一个帖子的 URI 不一定指示它正在应用的资源(但它可以)。


编辑: 另一个事情 - 一个 PUT 可以创建,但如果这样做,那么 ID 必须是一个自然 ID - AKA 一个电子邮件地址。

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

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