Web-development – the best way to implement different views of one resource in RESTful manner

apirestweb-development

Imagine, there is a resource, for example event. User is able to get list of events in HTML format. He should be able to view that list in two ways: as a list and as a calendar. How the API should respond to this options?

The simplest approach, I think, is to pass some parameter to index action to determine which type of presentation to use, but I don't like it. Don't know why, it is not RESTful for me.

What do you think, do I misunderstood something and should keep things simple or there is a better approach?

Best Answer

If the content is identical, it's unclear what "differences" would be seen in list vs. calendar. If the result is a JSON or XML document, there should be no discernible difference.

if the result is an HTML page, where formatting is part of the results, then the path is the correct thing to do, since the content is different.

You have several choices.

  1. Path changes. "/path/to/resource/view/id/" works well for us. Our views include "html", "xml" and "json".

    However, your "list" vs. "calendar" may or may not be the same content, in the a single format, and a different organization. So you have a number of issues tangled up together in your question.

  2. Query String changes. This: "?view=calender" or "?view=list".

  3. Fragment changes. This: "#calendar" or "#list". These can work well in your case, since the two views seem to have different content.

Related Topic