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

后端 未结 2 1957
渐次进展
渐次进展 2021-02-02 17:39

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

/resources/{id}

However there is not resource with the given id st

相关标签:
2条回答
  • 2021-02-02 18:19

    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.

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

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

    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

    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.

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

    It depends.

    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.

    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

    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. [...]

    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. [...]

    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. [...]


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

    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 Not Found

    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. [...]


    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

    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):

    [...]

    • Creating a new resource that has yet to be identified by the origin server;

    [...]

    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).

    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 Created

    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. [...]

    0 讨论(0)
  • 2021-02-02 18:30

    In short, it depends wheter the payload you want to store violates any constraint the server has for resources or not.

    In general I'd say it should attempt it as the client explicitly expresses his intent to store that particular representation at the target URI. The server should though perform constraint checks before! Usually, in a real REST scenario though, the client should use URI that are provided by the server and not just chose any URI on its own. Thereby, a server should be in control of its namespace, as such using PUT to create resources is not recommended here by default.

    With that being said, as PUT is idempotent while POST being not, some clients might want to benefit from this property. Here a POST-PUT creation pattern has evolved, where a client is attempting to create a new resource via POST until it receives a confirmation via a Location header in the response and afterwards attempts the update of that resource's state via PUT. This way the client can be sure that in case of transmission problems the representation was only created once. Depending on the stance, some people might consider the actual update of the resource as the actual resource creation, though as the client beforehand received the respective link, this is not quite the case.

    Note that a server also has the right to transform the representation to something different if i.e. the server is configured to provide specific representations for certain URI endpoints. Think of uploading an image via PUT to a URI and the server embedds the image into a HTML page

    0 讨论(0)
提交回复
热议问题