Debian Codegraph @

www/src/content/essays/hypermedia-clients.md

raw symbols SCIP
1 ---
2 title: "Hypermedia Clients"
3 description: "In this essay, Carson Gross explores the critical but often overlooked role of hypermedia clients in REST architectures. He explains why adding hypermedia controls to JSON APIs is insufficient for true RESTful systems without proper client implementation. Carson examines the challenges of building hypermedia clients and argues that web browsers remain the most practical choice for hypermedia-driven applications."
4 created: 2023-01-28
5 modified: 2023-01-29
6 authors: ["Carson Gross"]
7 tags: ["foundations"]
8 ---
9
10 Often, when we are being insufferably pedantic in [online discussions](https://news.ycombinator.com/item?id=32141027)
11 about [REST](/essays/how-did-rest-come-to-mean-the-opposite-of-rest) & [HATEOAS](/essays/hateoas), we will
12 say something along the lines of this:
13
14 > JSON isn't a hypermedia because it doesn't have hypermedia controls.
15 >
16 > Look at this JSON:
17
18 ```json
19 {
20 "account": {
21 "account_number": 12345,
22 "balance": {
23 "currency": "usd",
24 "value": 50.00
25 },
26 "status": "open"
27 }
28 }
29 ```
30
31 > See? No hypermedia controls.
32 >
33 > So this JSON isn't a hypermedia, and, therefore, the API returning this JSON isn't RESTful.
34
35 To this, occasionally, a smart and experienced web developer will reply with something along these lines:
36
37 > OK, mr. REST-y pants, how about this JSON?
38
39 ```json
40 {
41 "account": {
42 "account_number": 12345,
43 "balance": {
44 "currency": "usd",
45 "value": 50.00
46 },
47 "status": "open",
48 "links": {
49 "deposits": "/accounts/12345/deposits",
50 "withdrawals": "/accounts/12345/withdrawals",
51 "transfers": "/accounts/12345/transfers",
52 "close-requests": "/accounts/12345/close-requests"
53 }
54 }
55 }
56 ```
57
58 > There, now there are hypermedia controls in this response (normal humans call them links, btw) so this JSON is a
59 > hypermedia.
60 >
61 > So this JSON API is now RESTful. Feel better?
62
63 😑
64
65 One must concede that, at least at a high-level, our online adversary has something of a talking point here: these
66 do appear to be hypermedia controls, and they are, in fact, in a JSON response. So, couldn't you call this JSON response
67 RESTful?
68
69 Being obstinate by nature, we still wouldn't be willing to concede the immediate point without a good
70 [ackchyually](https://i.imgur.com/DpQ9YJl.png) or two:
71
72 * First, these links hold no information about what HTTP method to use to access them
73 * Secondly, these links aren't a *native* part of JSON the way that, for example, anchor and form tags are with HTML
74 * Third, there is a lot of missing information about the hypermedia interactions at each end point (e.g. what data needs
75 to
76 go up with the request.)
77
78 And so on: the sorts of pedantic nit-picking that makes technical flame wars about REST on the internet such a *special*
79 joy.
80
81 However, there is a deeper [ackchyually](https://i.imgur.com/DpQ9YJl.png) here, and one that doesn't involve the *JSON
82 API*
83 itself, but rather the other side of the wire: the *client* that receives the JSON.
84
85 ## Hypermedia Clients & Presentation Information
86
87 The deeper problem with this proposed fix for non-RESTful JSON APIs is that, for this JSON response to participate
88 properly in a [hypermedia system](https://hypermedia.systems), the *client* that consumes the JSON needs to *also*
89 satisfy the [constraints](https://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm) that the
90 RESTful architectural style places on the entire system.
91
92 In particular, the client needs to satisfy
93 the [uniform interface](https://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm#sec_5_1_5),
94 in which the client code knows nothing about the "shape" or details of the response beyond the ability to display
95 the given hypermedia to a user. In a properly functioning RESTful system, the client isn't allowed to have any
96 "out of band" knowledge about the domain that a particular hypermedia representation, er, represents.
97
98 Let's look at the proposed JSON-as-hypermedia again:
99
100 ```json
101 {
102 "account": {
103 "account_number": 12345,
104 "balance": {
105 "currency": "usd",
106 "value": 50.00
107 },
108 "status": "open",
109 "links": {
110 "deposits": "/accounts/12345/deposits",
111 "withdrawals": "/accounts/12345/withdrawals",
112 "transfers": "/accounts/12345/transfers",
113 "close-requests": "/accounts/12345/close-requests"
114 }
115 }
116 }
117 ```
118
119 Now, a client of this API *could* use a generic algorithm to transform this JSON into, for example, some HTML. It could
120 do so via a client-side templating language that, for example, iterated over all the properties of the JSON object.
121
122 But there's a hitch: note that there isn't a lot of *presentation information* in the JSON response. It is a fairly raw
123 data representation of the account in question, with a few additional URLs.
124
125 A client that wanted to satisfy the uniform interface constraint of REST doesn't have much information on how to present
126 this data to a user. The client would, therefore, need to adopt a very basic approach to displaying this account to an
127 end user.
128
129 It would probably end up being roughly a set of name/value pairs and a set generic of buttons or links for actions,
130 right?
131
132 There simply isn't much more it could do while remaining agnostic about the form of the JSON response.
133
134 ### Pushing Our JSON API Further
135
136 We could fix this by making our JSON API more elaborate and start including more information on how to lay out the
137 information: perhaps indications that some fields should be emphasized, or hidden, etc.
138
139 But that would only be part of the story.
140
141 We would also need to update the client side to interpret these new elements of our JSON API properly. So we are no
142 longer just API designers: we are getting in to the hypermedia *client* creation business as well. Or, more likely, we
143 are asking our *API clients* to get into the hypermedia client business as well.
144
145 Now, Mike Amundsen has written
146 an [excellent book](https://www.oreilly.com/library/view/restful-web-clients/9781491921890/) on
147 how to build a proper, generic hypermedia client. But what you will see in that book is that creating a good hypermedia
148 client isn't trivial, and, further, it is certainly not what *most* engineers would build to consume a JSON API, even if
149 the JSON API had increasingly elaborate hypermedia controls and presentation information in their responses.
150
151 ### "Inefficient" Representations
152
153 As we begin to consider adding more information to our JSON response, a quote from Roy Fielding's dissertation jumps
154 to mind:
155
156 > The trade-off, though, is that a uniform interface degrades efficiency, since information is transferred in a
157 > standardized form rather than one which is specific to an application's needs.
158
159 _-Roy Fielding, <https://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm#sec_5_1_5>_
160
161 A criticism of HTML is that it mixes "presentation" information with "semantic" information. This is often contrasted
162 unfavorably with the brevity of typical JSON API responses.
163
164 It turns out, however, that it is exactly that presentation information, and the ability of a web browser (i.e. a
165 hypermedia client) to
166 turn it into a UI that a human can interact with, that makes HTML work so well as a component of the larger hypermedia
167 system that is the web.
168
169 And that exactly what we find ourselves adding to our own JSON API to support a proper hypermedia client.
170
171 ## Building Hypermedia Clients
172
173 So, you can see, just offering hypermedia controls in a JSON API response isn't enough. It is *part* of the REST story,
174 but not the entire story. And, I have come to understand, it is not really the *hard* part of the story. In fact,
175 creating the
176 hypermedia *client* is the hard part, and creating a *good* hypermedia client is *the really hard part*.
177
178 Now, we are all used to web browsers just being there, but think for a moment about all the technology that goes in to
179 simply
180 parsing and rendering HTML to an end user in a normal, every day web request. It's *extremely* complicated.
181
182 That's why, if we want to build web-based [hypermedia-driven applications](/essays/hypermedia-driven-applications),
183 it's probably a good idea to use the standard, web-based hypermedia client: the browser.
184
185 It is already an extremely powerful, well tested hypermedia client. And, [with a bit of help](/docs),
186 it can be an even better hypermedia client.
187
188 In general, building a good hypermedia client that satisfies all the constraints of REST is hard, and we should lean
189 towards using (and extending) existing clients rather than building our own new ones.
190
191 ### Hyperview
192
193 That being said, there are times when building a new hypermedia client is appropriate. For example, this is what makes
194 a technology like [Hyperview](https://hyperview.org/) so impressive and special. Hyperview
195 doesn't just provide a specification for a new, mobile-friendly
196 hypermedia, [HXML](https://hyperview.org/docs/guide_html).
197
198 It also provides developers with a hypermedia *client* that understands how to render HXML.
199
200 Without that hypermedia client, Hyperview would be just another hypermedia-in-theory, like the JSON above, rather
201 than a compelling, practical and *complete* RESTful hypermedia solution.
202
203 A hypermedia without a hypermedia client is like a fish without a bicycle, except where the fish is really only good at
204 bicycling.
205
206 ## Conclusion
207
208 It took me a long time to appreciate how important the *client* is to a proper, RESTful hypermedia system. This is
209 understandable,
210 since most of the early discussion around REST was
211 around [API Design](https://www.martinfowler.com/articles/richardsonMaturityModel.html),
212 and the client simply didn't come up much.
213
214 What I see now is that a lot of these discussions were putting the cart before the horse: the only way a RESTful
215 hypermedia
216 API can be useful is if it is consumed by a proper hypermedia client. Otherwise, your hypermedia controls are wasted
217 on what is, at the end of the day, a domain-specific thick client that just wants to get things done.
218
219 Further, your hypermedia API is almost certainly going to have to carry a fair amount of presentation-layer information
220 in it to make the whole thing usable. It turns out that "Level 3" of the
221 [Richard Maturity Model](https://martinfowler.com/articles/richardsonMaturityModel.html), Hypermedia Controls, *isn't*
222 enough to reach "The Glory of REST".
223
224 In practice, you are going to need to add in a bunch of practical presentation-level technology to make
225 your hypermedia API really work, *and* you are going to need a properly built hypermedia client to consume it.
226
227 I had a nascent sense of this when I
228 wrote [HATEOAS Is For Humans](https://intercoolerjs.org/2016/05/08/hatoeas-is-for-humans.html),
229 but I didn't, at that time, appreciate just how special the client/web browser was.
230
231 REST isn't solely about APIs: as Roy Fielding makes clear in his dissertation, it is a *system* architecture.
232
233 Except for a few people like [Mike](https://training.amundsen.com/), we've been largely ignoring a larger (really,
234 *much* larger) part of the REST story:
235
236 <div style="text-align:center;padding-top: 24px">
237
238 <img src="/img/creating-client.png" alt="Creating A Hypermedia Client Is Hard Joke" style="max-width: 95%">
239
240 </div>