How to model hierarchical relationship in rest api?
Let’s say I want to add photo in an album, I can do this.
1
POST http://www.example.com/photo
POST http://www.example.com/album
POST http://www.example.com/addPhotoToAlbum?photoId={photoId}&albumId={albumId}
or I can do it in this way
2
POST http://www.example.com/album/{albumId}/photo/
But this way it will expose my server architecture.
or
3
POST http://www.example.com/album/addPhoto/albumId={albumId}
or
4
POST http://www.example.com/album/addPhoto/
with Json string {“albumId”:123, “photoId”:43543}
or
5
POST http://www.example.com/album/photo?photoId={}&albumId={}
REST Api is good for CRUD operation, but when it comes to how to establish relationship,
If we are following object oriented style, #2 is great. if we are following database style, #5 is great
If we are using style #2, when we want to add comments to a particular photo, should we write this
POST http://www.example.com/album/{albumId}/photo/{photoId}/comment/
or just
POST http://www.example.com/photo/{photoId}/comment/
Question: What is the general practice of establishing relationship in REST?
Here are some usefull points that help identify situations.
http://redrata.com/restful-uri-design/
Personally I like it when the URLS are short and ability to bookmark.