如果HTTP PUT不存在,应该创建一个资源吗? [英] Should HTTP PUT create a resource if it does not exist?

查看:220
本文介绍了如果HTTP PUT不存在,应该创建一个资源吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们假设有人对我的Endint执行PUT请求:

Lets suppose that someone performs a PUT request on my endoint:

/resources/{id}

但是我的PostgreSQL数据库中没有存储具有给定id的资源.

However there is not resource with the given id stored in my PostgreSQL database.

根据 RFC 2616 ,如果我有能力:

PUT方法请求将包含的实体存储在提供的Request-URI下.如果Request-URI引用了已经存在的资源,则应将封闭的实体视为原始服务器上的资源的修改版本.如果Request-URI没有指向现有资源,并且请求用户代理能够将该URI定义为新资源,则原始服务器可以使用该URI创建资源.

The PUT method requests that the enclosed entity be stored under the supplied Request-URI. If the Request-URI refers to an already existing resource, the enclosed entity SHOULD be considered as a modified version of the one residing on the origin server. If the Request-URI does not point to an existing resource, and that URI is capable of being defined as a new resource by the requesting user agent, the origin server can create the resource with that URI.

可以使用提供的ID创建资源吗?最好不要在数据库插入上手动分配ID.

Would be okay to create the resource with the provided id? As manually assigning ids on database insert is not the best practice.

如果无法创建资源,是否应该返回404错误?

Should I return a 404 error if the creation of the resource is not possible?

推荐答案

首先,您使用的是陈旧文档:RFC 2616如今已不再适用,任何使用该文档作为参考的人应该立即停止.

First of all, you are using an obsolete document: The RFC 2616 is no longer relevant nowadays and anyone using such document as reference should stop right away.

引用马克·诺丁汉,他在撰写本文时-主持IETF HTTP和QUIC工作组:

Quoting Mark Nottingham who, at the time of writing, co-chairs the IETF HTTP and QUIC Working Groups:

请勿使用RFC2616 .从硬盘驱动器,书签中将其删除,并刻录(或负责地回收)打印出的所有副本.

Don’t use RFC2616. Delete it from your hard drives, bookmarks, and burn (or responsibly recycle) any copies that are printed out.

以下定义了HTTP/1.1协议的文档已取代了旧的RFC 2616:

The old RFC 2616 has been supplanted by the following documents that, together, define the HTTP/1.1 protocol:

  • RFC 7230: Message Syntax and Routing
  • RFC 7231: Semantics and Content
  • RFC 7232: Conditional Requests
  • RFC 7233: Range Requests
  • RFC 7234: Caching
  • RFC 7235: Authentication

如果要查找方法,状态代码和标头定义,则 RFC 7231 是您应该参考的文件.

If you are looking for methods, status codes and headers definitions, then the RFC 7231 is the document you should refer to.

话虽如此,让我们回到您的问题.

Having said that, let's go back to your question.

如果HTTP PUT不存在,应该创建一个资源吗?

Should HTTP PUT create a resource if it does not exist?

要视情况而定.

但是,如果您的应用程序代表您的客户端生成资源标识符,就像您在问题中提到的那样,那么您应该使用 PUT 用于创建资源.

But, if your application generates resource identifiers on behalf of the client, as you mentioned in your question, then you should use POST instead of PUT for creating resources.

PUT 方法定义的某些部分在下面引用.最后一句话似乎与您最相关(突出之处是我的观点),支持我上面刚刚提到的内容:

Some parts of the PUT method definition are quoted below. The last sentence seems to be the most relevant to you (highlight is mine), supporting what I've just mentioned above:

4.3.4. PUT

PUT方法请求创建目标资源的状态或将其替换为由请求消息有效负载中包含的表示形式定义的状态. [...]

The PUT method requests that the state of the target resource be created or replaced with the state defined by the representation enclosed in the request message payload. [...]

如果目标资源没有当前表示,并且PUT成功创建了一个表示,则原始服务器务必通过发送201(已创建)响应来通知用户代理.如果目标资源确实具有当前表示形式,并且该表示形式已根据所包含表示形式的状态成功进行了修改,则原始服务器务必发送200(OK)或204(No Content)响应至表示请求已成功完成. [...]

If the target resource does not have a current representation and the PUT successfully creates one, then the origin server MUST inform the user agent by sending a 201 (Created) response. If the target resource does have a current representation and that representation is successfully modified in accordance with the state of the enclosed representation, then the origin server MUST send either a 200 (OK) or a 204 (No Content) response to indicate successful completion of the request. [...]

PUT请求的正确解释假定用户代理知道需要哪个目标资源. 代表客户选择适当URI的服务,在收到状态更改请求后,应该使用POST方法而不是PUT来实现. [...]

Proper interpretation of a PUT request presumes that the user agent knows which target resource is desired. A service that selects a proper URI on behalf of the client, after receiving a state-changing request, SHOULD be implemented using the POST method rather than PUT. [...]


如果无法创建资源,是否应该返回404错误?

这似乎是要返回的准确状态代码,因为未找到所请求资源的表示形式:

That's seems to be an accurate status code to be returned, as no representation has been found for the requested resource:

6.5.4.找不到404

404(未找到)状态代码表示原始服务器未找到目标资源的当前表示,或不愿意透露该资源的存在. [...]

The 404 (Not Found) status code indicates that the origin server did not find a current representation for the target resource or is not willing to disclose that one exists. [...]


为了完整起见,现在请在 POST 方法定义,在问题描述的场景中应使用该方法来创建资源:


Now, for the sake of completeness, find below some relevant quotes on the POST method definition, which should be used to create resources in the scenario described in your question:

4.3.3. POST

POST方法请求目标资源根据资源自身的特定语义来处理请求中包含的表示形式.例如,POST用于以下功能(以及其他功能):

The POST method requests that the target resource process the representation enclosed in the request according to the resource's own specific semantics. For example, POST is used for the following functions (among others):

[...]

  • 创建尚未由原始服务器识别的新资源;

[...]

如果由于成功处理了POST请求而在原始服务器上创建了一个或多个资源,则原始服务器应发送包含Location标头字段的201(已创建)响应,该响应应提供创建的主要资源的标识符,以及在引用新资源时描述请求状态的表示形式.

If one or more resources has been created on the origin server as a result of successfully processing a POST request, the origin server SHOULD send a 201 (Created) response containing a Location header field that provides an identifier for the primary resource created and a representation that describes the status of the request while referring to the new resource(s).

201 状态码表示有新资源已创建, Location 标头指示新创建的资源的位置位于.如果没有提供 Location 标头,则客户端应假定资源由有效请求URI标识:

While the 201 status code indicates that a new resource has been created, the Location header indicate where the newly created resource is located. If no Location header is provided, then the client should assume that the resource is identified by the effective request URI:

6.3.2. 201已创建

201(已创建)状态代码表示请求已得到满足,并导致创建了一个或多个新资源.由请求创建的主要资源由响应中的Location头字段标识,或者,如果未接收到Location字段,则由有效请求URI标识. [...]

The 201 (Created) status code indicates that the request has been fulfilled and has resulted in one or more new resources being created. The primary resource created by the request is identified by either a Location header field in the response or, if no Location field is received, by the effective request URI. [...]

这篇关于如果HTTP PUT不存在,应该创建一个资源吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆