Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

REST API: returning newly create resource id?

Tags:

rest

In terms of good REST API design, is it a good idea to return id of the newly created resource? Say I have an API:

api/resource POST

I've seen some guru who has such API to return empty json and insert the URI into with Location header in response. I think returning

{ 'id': '1000' } 

so that the caller can do something right away with it is better. Save one more round-trip to the server. What's the problem with this approach if any?

like image 255
lang2 Avatar asked Mar 16 '23 01:03

lang2


1 Answers

As requested I reposted my comment as an answer and added some more details to be worth an answer at all.

The common approach for creating new resources via HTTP POST method is to return a 201 Created status code. The response furthermore should therefore contain the current state of the resource as entity body and a location header pointing to the URI the resource can be found. Not doing so, may break some clients and prevent their correct interaction with the service. Clients here may be browsers, tailor made "languageOfYourChoice" applications or even other services.

In an ideal RESTful world, the client is only aware of one single URI to start with (like the entrance URI of the service, f.e. http://service.company.com/). The response of an invocation of that URI should return a document which can be understood by the client and contains further links the client can use if interested. This concept is similar to the big brother HTML and similar web resources used everyday by humans where links can be used to move from the start page to sub-pages or even external resources or other hyper-media resources. REST is just an abstraction and a generalization of the Web thing.

Although not part of the actual question and this is rather opinion based, there is an ongoing discussion among RESTful devolopers regarding the usefulness of common vs. specialized document types (mime types, ...). I think that there should be a standardized common REST-aware format for JSON, XML, ... Just plain application/json is not enough as this does not define how links and schemas have to be included. Even HAL is not descriptive enough (IMO) regarding executable actions on URIs and what document formats to expect on invoking those URIs and what arguments needed to be passed to the service.

Document formats (a.k.a media types) are one of the core concepts of Roy Fieldings acronym HATEOAS. Clients should not need any prior knowledge other than the entrance point and the media types understood by the clients (thus the previous paragraph). The possible actions are all described through following the HTTP protocol specification and knowing what the respective media types express.

Therefore, returning a URI instead of an ID has a couple of advantages:

  • URIs are per se unique, different vendors though may have identical IDs
  • Client needs no knowledge on how the server is creating those URIs, the server on the other hand can provide them almost for free. Returning a product ID of 1234 has almost infintly ways to be combined in an URI by the client - the client therefore needs special knowledge on how to create the URI so that the server is actually able to process the request correctly.
  • The server can move resources without the need of clients to change as clients (in an ideal RESTful world) would retrieve the URI of the resource within a further request' response. The client therefore needs only the semantical knowledge what the returned field acutally "means"
like image 165
Roman Vottner Avatar answered Mar 24 '23 12:03

Roman Vottner