views:

35

answers:

1

Hi!

Trying to get the grips of best URI practice in RESTfulness... Let's say that

  • /service/user.xml returns an xml with all users data
  • /service/user/1.xml returns an xml with data for user id 1

What if I want to get just the email address of the user(s)?

  • /service/user.xml?par=email or /service/user/email.xml for all users?
  • /service/user/1.xml?par=email or /service/user/email/1.xml for user id 1?

Regards / Jonas

+1  A: 

In my opinion, it makes sense if you think of it as constantly narrowing down your search. You first start with a huge collection of "objects", and refine, becoming more and more specific..

So it goes:

service -> choosing a particular user -> specify that user id -> specify what you need from them -> specify in what format you want that information

or service/user/:id/email.:format

Alternatively, it wouldn't be wrong to say service/user/:id.:format?parameter=:param, if it didn't make sense to create a whole route for something that is potentially trivial.

I think ultimately these specifics don't always have "the best way", but rather several "recommended ways", and one of them will suit you most.

You might find these links to be interesting.

Jeriko
Thank you, Jeriko! The narrowing approach seems to be the way to go! In my case /service/user/1/email.xml. Great!
Cambiata
I see the other answer was deleted.. I often wonder about the plural/singular here.. would like to hear some opinions in this regard, as I'm torn. I learnt RESTful design because of Rails, and they guide you toward `/users/` and `/users/1`, even though I intuitively expected `/users/` and `/user/1`. There's actually an option somewhere for `pluralized routes`.
Jeriko
Hmm, this is interesting stuff... Following your narrowing approach, pluralized versions would be reasonable: /users/ = all users, /users/1/ = narrowing all users to user id 1...
Cambiata
Agreed.. I might ask a new question about this some time, to get a few more opinions.
Jeriko