Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how should I handle subresources in REST?

Tags:

rest

I understood (I think...) the basics of REST but while trying to design my API I got a little confused with sub-resources, I'll try to explain with an example:

lets say I have an application that has two resources, users and rooms, a user can create rooms and join rooms, is the following REST URIs correct ?

  1. GET /users/< userID >/joinedRooms
  2. GET /users/< userID >/createdRooms

or should it be something like /users/< userId >/rooms/joined or /users/< userId >/rooms?query=joined ?

also how would "get similar rooms" URI would look like ? /rooms/< roomId >/similar or /rooms/?query=similar ?

like image 343
keisar Avatar asked Dec 13 '25 10:12

keisar


1 Answers

URL design is an art form, not a REST constraint. REST says nothing about what your URLs should look like. URLs are opaque strings and their contents is irrelevant.

  • /users/<userID>/joinedRooms
  • /users/<userID>/rooms/joined
  • /users/<userID>/rooms?query=joined

Any of these options you have suggested are equally valid. Pick whichever one works best for you, as long as it represents a resource.

also how would "get similar rooms" URI would look like

Again, whatever you want. How about /yaz1-oPhce6~X_? :-)

When making your decision, I suggest you try to factor in things like ease of implementation, aesthetics, and so on.

like image 119
Nicholas Shanks Avatar answered Dec 16 '25 23:12

Nicholas Shanks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!