6

I'm somewhat confused about which RESTful design is better.

For the API I have three types; Question, Answer and Student. The Question type has two subtypes; MultipleChoice and Open. The Question type will possibly have more subtypes in the future. Note that the subtypes have common and distinct (optional) properties. Answer is applicable to all questions.

I have seen similar questions (Modeling Object Inheritance in a REST Api, How to model a RESTful API with inheritance?), the given answers point to using a generic URL and specify a type in the body. However I do not feel as if the answers are authoritative (no reference to best practices, not a lot of upvotes, etc).

I'm looking for an authoritative, credible answer based on facts, not opinions.

I'll explain the possible designs below.

Type in URL

Requesting a list of all questions could be possible using GET /questions. This would return a JSON list containing a summary of questions:

[
  {
    "url": "http://example.com/questions/multiplechoice/1",
    "name": "example question"
  },
  {
    "url": "http://example.com/questions/open/2",
    "name": "another question"
  }
]

Requesting a list of multiple choice questions is possible using GET /questions/multiplechoice.

Creating a new multiple choice question is possible using POST /questions/multiplechoice

Server side these URLs map to different request handlers. The benefit of this is that there is no inspection necessary to check what type is to be created/returned, it is implied in the URL.

The downside of this approach (I think) is that when a student submits an answer the URL would be different for questions. POST /questions/multiplechoice/1/answers and POST /questions/open/2/answers.

Type in body

Requesting a list of all questions stays the same, GET /question. The output would also be the same.

Requesting a list of multiple choice questions would mean filtering so this will be GET /questions?type=multiplechoice.

Creating a new multipe choice question would mean also posting the type. The POST data would be:

{
  "type": "multiplechoice",
  "name": "example question"
}

The benefit would be that the URL remains simple and is less likely to change in the future. The downside is that one request handler needs to dispatch (using a mapping of some kind) requests based on body to other handlers specific to that type.

Answers are submitted using a universal URL: POST /questions/:question_id/answers

Type in header

The Content-Type and Accept headers can be used instead of the type parameter.

To get a list of all questions: GET /questions.

To get a list of all multiple choice questions: GET /questions, Accept set to application/app.multiplechoice+json.

This approach is similar to the type-in-body approach. The extra downside is that this involves custom mime types, which are not really obvious if you ask me.

Community
  • 1
  • 1
siebz0r
  • 18,867
  • 14
  • 64
  • 107
  • 1
    Unfortunately, as Andres said, API design is very subjective. To give some good examples though maybe you could provide more information, for example, would you ever need to submit multiple questions of different types at once? Also do you ever need to collate all students answers to a single question? This impacts whether answer is a sub resource of question or something completely different that only has a link to a question. – James Feb 14 '14 at 11:25
  • @James Questions are not created in bulk. I do not feel that this should impact the API design drastically. Answers are normally accessed though the question, this follows the domain design (an answer cannot exist without a question). Regardless of the subjectiveness of an API design there must be solutions to such 'generic' problems. (In software architecture there are design patterns, these are subjective yet can provide a solid foundation for a project when applied correctly.) – siebz0r Feb 14 '14 at 11:47
  • I totally agree and there are some good books and articles on the subject, but just as with other design patterns choosing the right one is the hard (and subjective) part. The problem you are trying to solve is a common one though; I'd refer to it as having multiple representations of the same resource; Also it's probably worth forgetting about the inheritance/object model and thinking more around the behaviors of those resources, if they happen to map to a hierarchical object model later, that's just a impl detail. I don't think I can give the answer you are looking for though; good luck! – James Feb 14 '14 at 13:57

2 Answers2

0

MultipleChoice and Open are subtypes of Question. So, there should be urls that contain /question/multiplechoice and /question/open.

About your wish of not receiving answers based on opinions, REST is not a standard, like JEE, nor an implementation, like Spring. It's a style, as baroque or gothic. So, you'll only get opinions as answers.

Andres
  • 10,561
  • 4
  • 45
  • 63
  • 2
    As the question states "I'm looking for an authoritative, credible answer based on facts, not opinions." – siebz0r Feb 14 '14 at 10:41
  • That doesn't mean that there are no proven implementations. Also, comments in the comments section please. – siebz0r Feb 14 '14 at 10:49
0

As per the Rest Api best Practice they given is best practice.In this the Rest is Resource based as per the standard we should use verb's so i think as per above url your answer is most prply same.

For Get
    E.g:- Get/questions/multiplechoice  //// this case controller/action/id id(optional)
                                             this route config will resolve this route
          Get/questions/open          
For post
    E.g:- Post/questions/multiplechoice //// you can use same route for post/put/delete.

          Post/questions/open