0

I am designing REST. I have user and user has contacts of different types. What should my endpoints be like according to REST?

This looks reasonable:

GET /users/:id/contacts
GET /contacts

On users endpoint I check contacts for another user and on contacts endpoint I check logged in user in contacts, but then if I need to get all contacts for all users I need to make a filter:

GET /contacts?user_id=:id

And make this endpoint return all contacts. And this makes endpoint on users redundant.

What is correct way to do it according to REST?

cassiomolin
  • 124,154
  • 35
  • 280
  • 359
pain.reign
  • 371
  • 2
  • 4
  • 17

1 Answers1

4

What should my endpoints be like according to REST? [...] What is correct way to do it according to REST?

That's a misconception.

The REST architecture doesn't enforce any URI design (see notes below). It's totally up to you to pick the URIs that better identify your resources.


The URI syntax is defined in the RFC 3986. As general rule, the path is organized in hierarchical form (with segments separated by /) and can contain non-hierarchical data in the query component (starting with ?).

So /users/{id}/contacts seems to be just fine to identify a collection of contacts that belongs to a particular user.


Note 1: The REST architectural style is described in the chapter 5 of Roy T. Fielding's dissertation and it defines a set of constraints that must be followed by the applications that follow such architecture. However it says nothing about what the URIs must be like.

Note 2: The examples of a popular article written by Martin Fowler explaining a model defined by Leonard Richardson suggest a URI structure that looks friendly and easy to read.

Community
  • 1
  • 1
cassiomolin
  • 124,154
  • 35
  • 280
  • 359
  • thanks a lot, could you tell a bit more how will it work all together regarding getting all contacts, getting contacts for logged in user and getting contacts for different user then logged in – pain.reign Apr 27 '18 at 15:01
  • 1
    @pain.reign That's totally up to you. Any suggestions I may give you will be primarily opinion-based. – cassiomolin Apr 27 '18 at 15:09