What is REST style for getSomethingByAnother () method
I have a Node.js REST API service and I have resouce - Link
for example.
- To get all the links I use
GET /links
- To submit a new link, I use
POST /links
- To get one link from a link I use
GET /links:id
Each link has an array Tags
and I need a REST style URI to get links by tag. What would be the REST style URI in this case?
source to share
I think it should be:
GET /links/:id/tags
which should return all tags associated with the link with id: id:
If you like working with your tags as a separate thing, you can do it like this:
GET /tags
==> get all tags.
GET /tags/:id
==> get tag with id.
GET /tags/links/:id
Also resftull is not strict and several times the resource or action you need to perform doesn't fit into this scheme and you can create your own method:
GET /tags/get-for-link-id/:id
=> get tags associated with a link
This example is pointless, but consider that you have a complex route with so many parameters, for example:
GET /tags?q=return&state=active&sort=date
If this request is repeated so many times, it would be nice for your api client to have a custom alias like GET /tags/activeByDate
source to share
It depends. You can do something like /blah:{tagId}/
(which is also a perfectly valid URI). Or you can do /links/?tagID={id}
and so on. I don't prefer the hierarchical URI already mentioned /tags/{id}/links
, I think a flat URI is much better. If the relationship has attributes as well, you can use /tag-link-relationships/{relationshipId}
either /tag-link-relationships/tag:{tagId}/
or /tag-link-relationships/?tag={tagId}
, etc.
From the client's point of view, the URI structure is irrelevant as the client follows the hyperlinks that the service responds to (aka HATEOAS unified interface / constraint).
off: Almost every day we got this question, at least we will try to search next time.
source to share