by dfabulich · 5y ago · 0 comments
Unfortunately, this article demonstrates what the industry has known/shown for years: that HATEOAS is unsuitable as a technique for HTTP APIs.
Imagine writing a programmatic client against the bank account "API" documented in this article, where the way you'd find out that you can't make a withdrawal is not by checking a "status" in JSON and finding it's overdrawn, but by parsing the HTML and noticing that there's no action link (no `<form>` element!!) that happens to make a withdrawal.
Instead of a JSON parser, you'd now need an HTML parser (one of the most notoriously quirky formats in modern use), but not just that, you'd also need your client to read and understand that HTML.
For example, where's the error message explaining why you can't withdraw? Why, it's written in the page as user-visible text. Which text is user-visible? Uh oh, now you need to parse and execute the CSS on the page to figure that out, too.
You'd need a full-blown browser to do that--a "user agent" as the HTML specification likes to call it--and now, instead of using an API, you're screen-scraping the web site by automating a browser with Selenium or Puppeteer or what have you. What will the client do when the server decides to restructure the HTML? Which one of those <a> links or <form> elements represents withdrawing cash??
It is possible to develop useful tools that automate full-blown websites, but it it's much much easier to develop useful clients when the responses are highly limited in the structure they're allowed to return, to couple the client to the server by committing, in advance, to how the API will work.
That's been Fielding's mistake from the very beginning: to think that hypertext provides useful techniques for developing programmatic interfaces. Hypertext is only useful because of its users. Users can read text, understand it, and decide what links to click on or what forms to submit. Programmatic API clients can't afford that luxury.
As the article demonstrates, if the client was intended to just be a program, we would never use HTML for any of these exchanges, and especially not use <a> links or <form> elements to convey to that program what is or is not possible.
The industry has rejected HATEOS not because we were too stupid understand how awesome it is; we've rejected it because we do understand how awesome it isn't.
It is extremely suitable, and, in fact, is simply descriptive, of an HTML-based API, however.
A lot of my thinking around this comes from my work on htmx, a tool that turns HTML into a richer hypermedia:
And I am actively encouraging people to try to split their thinking about JSON Data APIs (RPC) and REST-ful HTML-based systems.
Do you, though? The GP is pointing out that HATEOAS (terrible name btw -- why not just HAS, Hypertext Application State?) requires a full, stateful user agent for a client to consume the "API", whether human or otherwise. This is what we call "screen-scraping", and it's a pretty tough road to consume an API like that. In particular the GP points out that an interaction requires, in general, a (stateful) crawl of the remote. Note that screen-scraper issues boil down to state (which the GP covered) or "entropy" -- HTML is higher entropy than JSON, and programmers prefer to use the lowest entropy thing, because its much easier to work with, in exactly the same way text is easier to work with than a jpg of text.
What JSON driven SPAs are doing is reifying hypermedia using a static function that maps from JSON to HTML -- and that function IS coupled to the domain. And I would argue you can't get away from that as long as you want to decouple presentation from state - and it seems like HAS wants to explicitly couple them, which is bad news for programmatic consumption because it means changing the page layout will change your API, and that's called "brittle" in engineering circles.
Interestingly, you could rescue a low-entropy representation in HAS by specifying a (hypermedia-specific) function that does the reverse operation of the JSON SPA function, and extracts a low-entropy representation of the page. But this would impose constraints on page authoring, so it's not exactly without cost.
You and many others in this thread seem to think that an API must be separate from the front-end, which is understandable as that's the current standard way to do things, but HATEOAS is the polar opposite of that. The web itself is HATEOAS, so obviously it's useful.... the fact you can build a front-end app that consumes a JSON HTTP API on top of that shows just how incredibly powerful that concept is -but yeah, it requires a smart client, like a browser, to work...
We can argue all day, but the proof is in the pudding: build something real with this approach, and show me it's better. I have doubts, but even one counter example would change my mind.
That's hyperbole. If you're willing to accept clients which are hard-coded to some specific JSON structure, then we can accept clients which are hard-coded to some specific HTML structure.
The point is that a HATEOAS API will, by default and with no extra work from anyone, be browsable via any Web browser (including those with assistive tech like screen readers, etc.). In contrast, the best we have for JSON APIs is to pretty-print the response text, perhaps with syntax highlighting; embedded URLs might be made clickable, but we're probably restricted to only performing GETs.
If we use HATEOAS this might actually be enough for our purposes; e.g. if we just want to expose some basic functionality, like an admin UI. If not, we can write a client for that API; again, this doesn't require screen-scraping, or solving ambiguous parse issues, etc. since the client is not a general Web browser.
If we use JSON, we are pretty much forced to build a bespoke client for the API; due to how little we get 'by default'.
How does it differ, then? Do you have any code examples?
Based on your next statement:
If you're willing to accept clients which are hard-coded to some specific JSON structure, then we can accept clients which are hard-coded to some specific HTML structure
it does not differ, but you instead say that both approaches require hard-coded clients to consume programmatically, so they are equivalent.
If that were so, then another approach like embedding results in a jpg, would be equally valid since you just have to hard-code your clients to extract information from the jpg. After all the human can parse this so we don't need a bespoke client. From the perspective of theoretical movement of data from a to b, yes of course this is true. But from the perspective of an engineer consuming it, no, they are very different. And the difference is that entropy is proportional to developer pain.
I hope you try to be more rational and less ideological about your ideas. I'll leave it at that. I really like thinking about alternative architectures, but this one's appeal seems to rely on ignoring some very real trade-offs.
> Unfortunately, this article demonstrates what the industry has known/shown for years: that HATEOAS is unsuitable as a technique for HTTP APIs.
Yes - that's all in Fielding's thesis - the REST architecture style is derived explicitly for hypertext applications, and contrasted with among others RPC and "movable code" (ie: server send js to client to execute).
So yeah, most people seem to have misunderstood Fielding's thesis.
Now, if one wants to argue against the benefits of multi-layer caching and so on, one might hold up the complexity and relative failure of WebDAV. I'm not convinced that's a failure of architecture as much as a failure of the design process surrounding WebDAV, however.
If you want return some information that says why something is not available, you could do that in multiple different ways.
You could either simply return a piece of information that say 'Account below 0' or you could return a the link and suggest the client could do a HEAD against the endpoint.
It seems you have taken the example given here to literal.
Sometimes it totally correct not to want to return something the client can't do. The client doesn't always need an explanation why something is not available. Many clients should even know that something should be available.
If you always return all the reason for everything you are also leaking information.
> The industry has rejected HATEOS not because we were too stupid understand how awesome it is; we've rejected it because we do understand how awesome it isn't.
The reason is in my opinion that the extra effort required is often not worth it because most companies control the client and the server and simply evolve them together. HATEOS is most effective when you want to have multiple clients that all evolve separately.
Agree with shaunxcode; while HTML indeed has quirks (such as wrt inline script+CSS parsing and URLs), plus overly permissive error recovery, it's just straight SGML with formal tag inference and markup minimization techniques known since at least 1986 when SGML was published.
The problem IME with HATEOAS is that even highly capable devs see it as an optional, nice to have feature, when loose coupling and late discovery of links in hypertext is the entire point of Fielding's REST concept. It baffles me to no end that even gifted developers turn to exegetic and "best practice" approaches for justifying their JSON-over-HTTP backends and squeezing invocation parameters into URLs when all they really need is a JSON response that can be parsed by browsers, and often times pass JSON in backend-to-backend responses in non-Javascript environments not even having JSON as built-in serialization format.
What's more: the argument against HTML can be made for the soup that is used on the web-for-browsers; where webdevs put divs in spans in a-s in buttons to work around some quirk in CSS or such.
This does not go for the HTML that you design especially for an API. Such HTML is simple, clean, lean and flat, while being semantic. Parsing such HTML is easy: every language has a DOM parser for such XML.
But from my perspective, where things started breaking apart was with the advent of html5, when sloppy html really became the accepted norm.
Parsing xhtml wouldn't have been a problem, and I'm sure we'd eventually found out a better "skinning" language to use than xslt over the server's response.
I am still yet to come across a real HATEOS implementation in real life, and probably the chances are smaller by the day. It has this nice theoretical purity but I don't know yet if it's worth the complexity.
Sloppy html was the accepted norm long before html5.
It is not, which is why no one does it; solution in search of a problem.