Using HTML as the Media Type for your API
codeartisan.blogspot.co.uk
codeartisan.blogspot.co.uk
<color pattern="checked">blue</color>
or this: <color>
<pattern>checked</pattern>
<value>blue</value>
</color>
or even this: <color value="blue" pattern="checked" />
And lots of variations. JSON simplified everything into just maps and arrays, so there's one straightforward, simple way to do it.Using HTML for your API response re-introduces all the confusion as to what to put where, and now you've got to decide which random HTML tag to use for which element. For 99% of the cases, I see no benefit to it.
The only thing I think of is if you're returning a very large and complex data set, and you want to take advantage of jQuery/similar to use selectors to extract certain patterns of data, which could be a cool thing do to. Have to say, I've never needed that before, though.
... and by the way what if like most web designers I don't agree with the HTML layout of the data? For example I don't want list items in a bullet list, I want them in sortable table or in a combobox droplist instead. Then I have to convert the data into some readable structure so I can change it's layout, which means reading the data into a Javascript object perhaps ... gee, I wish the API just gave me JSON output in the first place.
The point is that this is not a minor benefit. You literally have documentation for your API built in. You don't need to write a whole set of docs listing what parameters are acceptable for which requests. You get the semantics of form handling and link-following for free. Having worked on an API that basically brings HTML form handling to raw JSON and XML (Twilio's), where we had to work hard to create documentation for what we were doing, I can tell you this is a big deal.
Incidentally, using a media type like HTML is the only way to build a true REST API. If more people understood that, we might be able to have a saner discussion around REST and its trade offs.
That would definitely be a worthwhile point to consider, if REST didn't have several completely impractical requirements that people inevitably abandon the moment they try to make their site do anything more complex than (unauthenticated) CRUD.
I'd have to say that the major impediment in that case is that there are no benefits.
$("#listItems li", apiResponse).each(function() {
$("#mytable").append("<tr><td>$(this).html()</td></tr>");
});
Json or html, you're going to have to navigate to the list in the api response and loop over the list items to generate the markup you want.You can't totally decouple the clients; they have to understand in some way the vocabulary the server is using, but you can do a lot to decouple them from the structure of the markup.
If we could agree to use some subset of HTML that would map to native objects in most languages, then we could build tools around that standard.
Or we could approach from the other side and just try to bolt on some hypermedia features to JSON, as projects like HAL are attempting to do. This seems to have more traction at the moment.
HAL is going for "start with something clean for machines (JSON and XML), layer in hypermedia, get people to adopt it." This seems like a perfectly reasonable approach.
I'm suggesting another approach, which is "start with something that supports hypermedia and is already ubiquitous on the web, and figure out how to get machines to make sense of it."
I'm not sure either one is easy, so it's worth having multiple horses in the race, I think.
H Factor
http://www.amundsen.com/hypermedia/hfactor/
Hypermedia-Oriented Design
http://amundsen.com/articles/hypermedia-oriented-design/
It's possible to develop and document a well-defined "semantic profile" for a hypermedia API that uses HTML as the base media type. For an example see:
ALPS - Application-Level Profile Semantics
http://amundsen.com/hypermedia/profiles/
By leveraging some or all of the details of the API, machine-to-machine logic can be developed fairly easily. In his recent book Building Hypermedia APIs with HTML5 and Node, Mike Amundsen demonstrates an example of a "microblogging bot" that consumes an API conforming to the semantic profile detailed in the ALPS document. The bot (written in JavaScript), knowing only the semantics involved with registration and "create post" links/forms, and an entry point for the API, is able to signup and begin making posts.
The interesting thing is that the precise implementation of the API is not important, e.g. whether it's written with NodeJS or PHP or Clojure or whatever. Also, from one implementation to another, the pages served by the API could vary in terms of the layout of the pages, CSS, etc. So long as the pages conform to the semantic profile, the bot should be able to work equally well with a wide variety of implementations.
Regarding JSON, there are a number of attempts to develop hypermedia-rich media types on top of it:
HAL
http://tools.ietf.org/html/draft-kelly-json-hal-03
JSON-LD
Collection+JSON
http://www.amundsen.com/media-types/collection/
Siren
https://github.com/kevinswiber/siren
There are others as well.