'REST conventions for a friends list
I have a database (ORM) with users which look like this:
UserEntity (
id: UUID,
name: String,
phoneNumber: String,
friends: List<UserEntity>
)
I also have the following friends endpoints:
GET /user/<userId>/friends (returns a list of UUID's for all friends the user has)
POST /user/<userId>/friends?friendId=<friendId> (saves a new friend to the friends list)
DELETE /user/<userId>/friends?friendId=<friendId> (deletes a friend from the friends list)
Now I asked myself: is this the correct way of implementing a RESTful friend list and what is a good REST convention to obtain the profile of a friend. Should the friend resource be reached from the following path:
GET /user/<userId>/friends/<friendId>
Or from this path:
GET /user/<friendId>
Both of the given endpoints from above give back the same resource (a profile) but which one should I choose? I am diving deeper into REST conventions but I didn't find any clear answer for this. I dont't even know if my current REST design is correct. Thanks in advance for your help.
Solution 1:[1]
What you are talking about I would call nice URL convention. https://en.wikipedia.org/wiki/Clean_URL It has nothing to do with REST https://www.ics.uci.edu/~fielding/pubs/dissertation/fielding_dissertation.pdf and I have no idea why people think it has. From the REST point of view it would be equally good to do something like GET|PUT|DELETE /dsgvs23w?x235rwef=1&aegs234523f=2 and decide which URL to follow based on the metadata that describes it. If you violate HATEOAS, then you don't have a REST API.
As of the nice URL convention I would do something like GET /friendships/?user=1 and GET|PUT|DELETE /friendships/1+2 and add some extra info about the friendship e.g. strength=BFF, duration=20y, etc. But there are infinite good solutions for this and it really depends on your needs.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | inf3rno |
