In what situation should the client choose a unique resource identifier for the REST URI and in which should the server indicate?

It looks like I can make my REST API. I can create users with POST without specifying a URI, and it will create the user and return the URI. Or I can create users using PUT and specify the URI itself.

When do you need to use another? The main difference is that in one method the MY system decides what the unique identifier is and, therefore, the URI for the resource, in another situation they determine what should be when I create.

+6
source share
4 answers

This mainly depends on whether you want to give up control of the naming of resources to the client.

The biggest problem is simply dealing with conflicts (if I am PUT / photo.png and you are PUT / photo.png, is this normal?).

Answer these questions and you are on your way.

+2
source

When your user specifies a resource identifier, he can DISABLE in the URI; the identifier that they execute PUT is the specification of the resource identifier.

When you specify a resource identifier, they can send a POST to the parent / group URI; your system will assign a URI to the resource and return it to the client so that they can refer to their created resource.

0
source

The answer to this question depends on two more specific questions:

  • Do customers know the location of the resource being created? (This may be the case if, for example, users are accessed by user name, and not by the identifier assigned by the server.)
  • Do customers have a complete view of the resource that will be created? (Perhaps this is not the case if some part of your resource is computed by the server.)

If the answer to both of these questions is yes, then PUT is probably appropriate. If you answered no to the fact that you should stick to POST.

0
source

I can create users using POST without specifying a URI, and this will create a user and return a URI OR I can create users using PUT and specify the URI itself .

When should you use another?

Use the first one.

In RESTful HTTP, the client should never create a URI. The service must be well connected, which means that the client should always only execute the URIs specified by the server and request these URIs.

It creates a better separation between client and server and makes it easy to make changes to the service without breaking existing clients.

(And yes, many existing APIs are wrong)

There's a really good Fielding article related to this topic here:

http://roy.gbiv.com/untangled/2008/rest-apis-must-be-hypertext-driven

0
source

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


All Articles