Should I use the POST request on / resources / or PUT request on / resources / id / to create a new object?

What is a RESTful way to create an object? Should I use the POST on / resources / URI and return the URI for the newly created resource in the response or use the PUT in / resources / id / URI? The GET request for / resources / id / will probably return 404, but should the PUT return 404? Should both methods be used to create a new object?

+4
source share
1 answer

Typically, you will use one or both depending on whether you want the client (and therefore the user) to determine the URI or not. If the client sends a resources/ message, then the server receives a URI definition for the resource. If the PUT client is on resources/{id}/ , then the client determines the URI for the resource.

The only exception is that if the creation includes links, states, and other elements that are then not considered part of the resource, you usually cannot use these additional “construction arguments” if you want, because they are not part of the state resource. Instead, you should do a POST in this case.

Even if you use POST to create, you might still want to open PUT for updates. It depends on the resource.

If you do not allow PUT to be created, then yes, you must return 404 in this situation.

+9
source

Source: https://habr.com/ru/post/1388789/


All Articles