Professional Documents
Culture Documents
8 May 2016
##TLDR##
HATEOAS - Wat?
HATEOAS (https://en.wikipedia.org/wiki/HATEOAS) is perhaps the
least understood aspect of REST
(https://en.wikipedia.org/wiki/Representational_state_transfer) and
is often an object of outright hatred
(https://je knupp.com/blog/2014/06/03/why-i-hate-hateoas/) for
perfectly reasonable, competent developers. This is too bad, and it
is mainly due to the fact that the HATEOAS and REST concepts have
been applied to APIs (originally XML, then JSON) rather than to
where they originally arose: humans and HTML on the web.
Simple!
‘Hypermedia As The Engine Of Application State’
What it means is: all “state” (that is, both the data and the network
actions available on that data) is encoded in hypermedia (e.g.
HTML) returned by the server. Clients know nothing speci c about
any particular network end point: both the data and the network
operations available on that data come from the server.
The crucial point, to repeat again: both the data and the network
operations on that data come from the server, together, in
hypermedia.
<div>
<div>
Name: Joe Blow
</div>
<div>
Email: joe@blow.com
</div>
<div>
<a href="/contacts/42/edit">Edit</a>
<a href="/contacts/42/email">Email</a>
<a href="/contacts/42/archive">Archive</a>
</div>
</div>
This bit of HTML, you will notice, encodes both the data for the
contact, as well as the actions available on that data (Editing,
Emailing and Archiving) in the form of links. The client (a browser)
knows nothing about contacts, it knows only how to take this HTML
and render it as some UI for a human to interact with. It’s certainly
not the most e cient encoding of this data, and it is intermixed
with some other junk as well, but that’s OK. That other junk has
proven to be pretty useful on the client side, so let’s let it slide for
now.
If you have ever built a traditional web app, congrats, you have
implemented HATEOAS better than 99% of all API developers.
What can it do with all those actions? The actions, note, are
dynamic, but the script itself probably isn’t: it needs to either handle
all possible actions or forward them along to a human to deal with,
right?
And that gets to crux of the issue: the code doesn’t (yet) have
agency (https://en.wikipedia.org/wiki/Agency_(philosophy)).
It can’t reasonably decide what to do in the face of new and
unexpected actions. The coder writing the code could have it handle
all possible actions (tough) or pass them along to a human
somewhere else (also tough).
Realistically, the code will likely handle a few of the actions and just
ignore the rest, so all that work for a Gold REST Star is,
unfortunately, wasted.
The server software knows all about the data and what actions are
available on that data, but has no idea what the heck to do.
Fortunately, these otherwise bumbling humans show up and will
poke and prod the server to provide the agency the server so
desperately needs. The server, of course, wants to speak with the
humans in a language (hypermedia) that the humans nd pleasant,
or at least tolerable.
Again, that’s HTML. I didn’t realize just how special it was until year
20 of writing web apps.
Once we have strong AI, maybe the situation changes. But that’s
what we’ve got today.
That’s of course correct and, I’m not ashamed to admit, I’m not
sure what the right answer is here, of if there is a single one.
REST-minus-HATEOAS seems like it works OK in many cases. RPC-
style end points were once popular and appear to be getting
popular again. They all seem reasonably workable to me.
But what I am convinced of, and what I hope to convince you of, is
that HATEOAS is largely wasted on machines.
LOG IN WITH
OR SIGN UP WITH DISQUS ?
Name
A web browser knows how to render HTML because it has been coded to do so. The fact
that it can render HTML pages that it did not know about before, is the only clever thing
happening. If you teach a hypermedia driven client how to pay for things, then it may be
capable of buying all kinds of things as long as they can be represented as a resource.
With or without human interaction.
In order for hypermedia to work, a client needs to have "special knowledge" of the media
types and all the link relation types it will interact with.
Additionally, hypermedia as the engine of application state is not just about returning links
in representations. It is also about building clients whose state can be manipulated by the
responses that are returned from a server. A client expresses intent by following a link
and the server changes the client state appropriately by sending a response.
△ ▽ • Reply • Share ›
I'd argue that the <a href= is even more interoperable as it's been a standard for
decades now.
△ ▽ • Reply • Share ›
As I say in the blog post: the human API can and should be designed with human
agency in mind. That is: it can be much more dynamic and self-explanatory
whereas the machine API needs to be more regular and more generally
expressive.
1△ ▽ • Reply • Share ›
ALSO ON INTERCOOLERJS.ORG
intercooler.js - Simple AJAX using HTML How It Feels To Learn Intercooler In 2016
attributes 5 October 2016