0

I read about REST API specifications. These are the principles:

  1. everything is a resource
  2. each resource is identifiable by a unique identifier
  3. use the standard HTTP methods

Now suppose there is a table for contact details

id , owner, contact name,contact number, created At

I want to design an API to consume the data. I can design the api in the following ways.

For getting the contact by owner

Get /contact/owner/david

or

Get /getContactByOwner?ownerName="david"

For writing into the table

post /contact/owner

 {contactDetail JSON in request param}

or

 post /addToContact?owner="john"&...

Which design is RESTful? What is wrong with the other one?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
rishabh dev
  • 1,711
  • 1
  • 15
  • 26
  • Is it wrong to ask for best practices and opinion of more experienced programmers? Is stackoverflow only for correcting syntax errors? @downvoter. – rishabh dev Jul 05 '16 at 05:46
  • *"Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise."* Also, there are duplicates: http://stackoverflow.com/q/1619302/3001761 – jonrsharpe Jul 05 '16 at 06:00
  • In my question I asked which design is restful. If both are restful then it is also a valid answer if backed by reason. Each possibility can be backed by facts and references which rest specification. In no way I am using ambiguous language. @jonrsharpe. – rishabh dev Jul 05 '16 at 06:34
  • I never said it was ambiguous, just that it was opinion-based and duplicated. There is no *"REST specification"*. – jonrsharpe Jul 05 '16 at 06:34
  • Rest specifications are a set of rules which is not opinion based. Either an api will restful or not. It is not I am asking which good or bad.@jonrsharpe – rishabh dev Jul 05 '16 at 06:37
  • There isn't a set of rules. *There isn't a REST specification"*. There are some principles, which you already have, and how best to implement them is a matter of opinion. **And it's duplicated**. I see no reason to waste further time on this. – jonrsharpe Jul 05 '16 at 06:46

1 Answers1

1

The rule of thumb with RESTful naming conventions is to use nouns as your endpoints (since your verbs should be limited to get / post / put / delete / etc). So in your example, Get contact/owner/david and Post contact/owner would be preferable. However, if you're really using REST architecture, you should technically be using HATEOAS (Hypertext-as-the-engine-of-application-state) and including links in XML or HTML responses, so if you're using JSON it's probably more REST-like as opposed to full-blown REST. At the end of the day, it's all a matter of preference; just try to use whatever will fit the needs of the application's users in a way that's somewhat self-documenting and intuitive.

Alec
  • 1,399
  • 4
  • 15
  • 27