4

I see in many different REST API docs the use of the me word like Imgur one. It enable this kind of route that looks very elegant:

GET  /users/{id}  -> Get public data from user {id}
GET  /users/me    -> Get my data if I'm connected

But is it actually 'REST valid'? Does it respect the REST convention?

johannchopin
  • 13,720
  • 10
  • 55
  • 101
  • What do you mean by "valid"? What makes you think that is not "valid"? – Nico Haase Apr 30 '20 at 21:22
  • "user" is a resource here, and "me" is a resource identifier, though in context of the logged in user. So, looks good to me. Difficult to argue if this is valid or not, as REST is just a set of guidelines which can be interpreted differently. – narendra-choudhary Apr 30 '20 at 21:26
  • @NicoHaase I'm sorry for the terms "valid". I mean does it respect the REST convention? For example one of the REST convention is to use plural so the `/user` route don't really respect it where `/users` do – johannchopin Apr 30 '20 at 21:26
  • @narendra-choudhary So there is no problem to use such 'fancy' resource identifier? – johannchopin Apr 30 '20 at 21:30
  • There is no `/user` route in your example, and there is no `/user` route on Imgur. And after all, it's the choice of the API designer to use plural or singular nouns – Nico Haase Apr 30 '20 at 21:30
  • @NicoHaase Yeah at the end it's always the choice of the API designer but in most case using plural make more sence. I really recommend your to read this answer https://stackoverflow.com/a/21809963/8583669 – johannchopin Apr 30 '20 at 21:37

2 Answers2

1

There's not really a 'valid REST', or 'standard REST'. REST isn't officially really tied to using HTTP.

Is this a reasonable way to design an API? I would say so. Does it violate conventions/common-sense/HTTP best practices? I don't think so.

The way you design your URL structure is entirely up to you. It's usually only frameworks that enforce stricter conventions, but it doesn't mean your approach is wrong.

Evert
  • 93,428
  • 18
  • 118
  • 189
1

The downside of context-bound URIs as /users/me is that they are not shareable. So I'd say it's fine to have one for convinience but only to complement a corresponding unique URI like /users/{id}.

jannis
  • 4,843
  • 1
  • 23
  • 53
  • Yeah and it's normal because we don't want to share this one. `/users/me` will only be called by the user which has a token and want to access his data. And how do you do to get the user id without using a tech like JWS? Only having `/users/{id}` need you to store the `id` from the current user somewhere locally. – johannchopin May 01 '20 at 11:11