Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rest API Architecture - GET a resource by alias

I need a help from you about REST Arch. I've a resource and I can retrieve it with the classical GET /resource/ID URI, but this resource has an alias and someone want to GET this resource by calling it via alias.

There is a good way to do so by calling a GET /resource/?alias=x, take the ID and then go to the details /resource/ID.

Do you have any good idea about other ways to do this?

like image 992
franco Avatar asked Sep 16 '25 07:09

franco


1 Answers

There is nothing wrong with a resource having two URIs (or two URIs pointing to the same resource, to put it another way). For example

GET www.myweatherapi.com/2013/11/18/rainfall

GET www.myweatherapi.com/today/rainfall

can both point to the same resource. You could say the latter is an alias of the former, or vice versa, it doesn't really matter, they both identify the same resource. You don't need to start explicitly labeling something as an alias of something else.

If the alias is temporary and may be gone in the future you could use the 307 response, temporary redirect. This tells the client that they should go to a different URI to find the resource, but not to assume that will be true in the future (eg limit how long you cache this).

As an aside, the client should not construct URIs, the server should return a content type format (HTML, JSON etc) that contains a way to identify the resources the client wants along with the URI of where to find them. For example a link in HTML saying "Todays Rainfall" with the URI to that resource. The user follows that link if they want todays rainfall

like image 103
Cormac Mulhall Avatar answered Sep 17 '25 22:09

Cormac Mulhall