Debian Codegraph @

www/src/content/essays/hateoas.md

raw symbols SCIP
1 ---
2 title: "HATEOAS"
3 description: "In this essay, Carson Gross explores HATEOAS (Hypermedia as the Engine of Application State), explaining how it enables REST APIs through hypermedia responses and contrasting it with modern JSON-based APIs. Using clear HTML examples, Carson demonstrates how HATEOAS allows clients to discover available actions dynamically through hypermedia rather than requiring prior knowledge of the API interface."
4 created: 2021-10-16
5 modified: 2022-02-06
6 authors: [ "Carson Gross" ]
7 tags: ["foundations"]
8 #show_title: false
9 #show_author: false
10 ---
11
12 <link rel="preconnect" href="https://fonts.googleapis.com">
13 <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
14 <link href="https://fonts.googleapis.com/css2?family=Lexend+Zetta:wght@900&display=swap&text=HATEOAS" rel="stylesheet">
15 <link href="https://fonts.googleapis.com/css2?family=Lexend+Zetta:wght@900&display=swap" rel="stylesheet">
16 <link href="https://fonts.googleapis.com/css2?family=Source+Serif+Pro:ital,wght@0,400;0,600;0,700;1,400;1,700&display=swap" rel="stylesheet">
17
18 <h1>HATEOAS</h1>
19
20 <section>
21
22 ## Preface: _HATEOAS &mdash; An Alternative Explanation_
23
24 This page is a reworking on the [Wikipedia Entry on HATEOAS](https://en.wikipedia.org/wiki/HATEOAS), which uses JSON.
25 Here we use HTML to explain the concept, and contrast it with JSON APIs. It is a more opinionated explanation of the
26 concept than would be appropriate for Wikipedia, but it is more correct in our opinion.
27
28 </section>
29
30 Hypermedia as the Engine of Application State (HATEOAS) is a constraint of
31 the [REST application architecture](https://en.wikipedia.org/wiki/Representational_state_transfer) that distinguishes it
32 from other network application architectures.
33
34 With HATEOAS, a client interacts with a network application whose application servers provide information dynamically
35 through [*hypermedia*](https://en.wikipedia.org/wiki/Hypermedia). A REST client needs little to no prior knowledge about
36 how to interact with an application or server beyond a generic understanding of hypermedia.
37
38 By contrast, today JSON-based web clients typically interact through a fixed interface shared through documentation via
39 a tool
40 such as [swagger](https://swagger.io/).
41
42 The restrictions imposed by HATEOAS decouples client and server. This enables server functionality to evolve
43 independently.
44
45 ## Example
46
47 A user-agent that implements HTTP makes a HTTP request of a REST end point through a simple URL. All subsequent requests
48 the user-agent may make are discovered within the hypermedia responses to each request. The media types used for these
49 representations, and the link relations they may contain, are standardized. The client transitions through application
50 states by selecting from links within a hypermedia representation or by manipulating the representation in other ways
51 afforded by its media type.
52
53 In this way, RESTful interaction is driven by hypermedia, rather than out-of-band information.
54
55 A concrete example will clarify this. Consider this GET request, issued by a web browser, which fetches a bank account
56 resource:
57
58 ```
59 GET /accounts/12345 HTTP/1.1
60 Host: bank.example.com
61 ```
62
63 The server responds with a hypermedia representation using HTML:
64
65 ```html
66 HTTP/1.1 200 OK
67
68 <html>
69 <body>
70 <div>Account number: 12345</div>
71 <div>Balance: $100.00 USD</div>
72 <div>Links:
73 <a href="/accounts/12345/deposits">deposits</a>
74 <a href="/accounts/12345/withdrawals">withdrawals</a>
75 <a href="/accounts/12345/transfers">transfers</a>
76 <a href="/accounts/12345/close-requests">close-requests</a>
77 </div>
78 <body>
79 </html>
80 ```
81
82 The response contains following possible follow-up actions: navigate to a UI to enter a deposit, withdrawal, transfer,
83 or to close request (to close the account).
84
85 Consider the situation at a later point, after the account has been overdrawn. Now, a different set of links are
86 available due to this
87 account status change.
88
89 ```html
90 HTTP/1.1 200 OK
91
92 <html>
93 <body>
94 <div>Account number: 12345</div>
95 <div>Balance: -$50.00 USD</div>
96 <div>Links:
97 <a href="/accounts/12345/deposits">deposits</a>
98 </div>
99 <body>
100 </html>
101 ```
102
103 Only one link is available: to deposit more money. In the accounts current overdrawn state the other actions are not
104 available, and
105 this fact is reflected internally in *the hypermedia*. The web browser does not know about the concept of an overdrawn
106 account or,
107 indeed, even what an account is. It simply knows how to present hypermedia representations to a user.
108
109 Hence we have the notion of the Hypermedia being the Engine of Application State. What actions are possible varies as
110 the
111 state of the resource varies and this information is encoded in the hypermedia.
112
113 Contrast the HTML response above with a typical JSON API which, instead, might return a representation of the account
114 with a
115 status field:
116
117 ```json
118 HTTP/1.1 200 OK
119
120 {
121 "account": {
122 "account_number": 12345,
123 "balance": {
124 "currency": "usd",
125 "value": -50.00
126 },
127 "status": "overdrawn"
128 }
129 }
130 ```
131
132 Here we can see that the client must know specifically what the value of the `status` field means and how it might
133 affect
134 the rendering of a user interface, and what actions can be taken with it. The client must also know what URLs must be
135 used
136 for manipulation of this resource since they are not encoded in the response. This would typically be achieved by
137 consulting documentation for the JSON API.
138
139 It is this requirement of out-of-band information that distinguishes this JSON API from a RESTful API that implements
140 HATEOAS.
141
142 This shows the core difference between the two approaches: in the RESTful, HATEOAS HTML representation, all operations
143 are encoded
144 directly in the response. In the JSON API example, out-of-band information is necessary for processing and working with
145 the remote resource.
146
147 ## Origins
148
149 The HATEOAS constraint is an essential part of
150 the ["uniform interface"](https://en.wikipedia.org/wiki/Representational_state_transfer#Uniform_interface) feature of
151 REST, as defined in Roy Fielding's [doctoral dissertation](https://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm).
152 Fielding's dissertation was a discussion of the
153 early web architecture, consisting mainly of HTML and HTTP at the time.
154
155 Fielding has further described the concept, and the crucial requirement of
156 hypermedia, [on his blog](https://roy.gbiv.com/untangled/2008/rest-apis-must-be-hypertext-driven).
157
158 ## HATEOAS and JSON
159
160 *NOTE: The Neutral Tone Of This Section is Disputed*
161
162 In the early 2000s the concept of REST was appropriated from its initial conceptual environment as a description of the
163 early web into other areas of web development: first XML API development (often
164 using [SOAP](https://en.wikipedia.org/wiki/SOAP)) and then JSON API development. This, despite the fact that neither XML
165 nor JSON was a natural hypermedia in the same manner as HTML.
166
167 In order to characterize different levels of adherence to REST in these new
168 areas, [The Richardson Maturity Model](https://en.wikipedia.org/wiki/Richardson_Maturity_Model) was proposed, consisting
169 of various levels of "maturity" of APIs, with the highest level,
170 Level 3, consisting of "Hypermedia Controls".
171
172 JSON is not a natural hypermedia and, therefore, hypermedia concepts can only be imposed on top of it. A JSON engineer
173 attempting to meet Level 3 of the Richardson Maturity Model might return the following JSON corresponding to the
174 bank account example above:
175
176 ```json
177 HTTP/1.1 200 OK
178
179 {
180 "account": {
181 "account_number": 12345,
182 "balance": {
183 "currency": "usd",
184 "value": 100.00
185 },
186 "links": {
187 "deposits": "/accounts/12345/deposits",
188 "withdrawals": "/accounts/12345/withdrawals",
189 "transfers": "/accounts/12345/transfers",
190 "close-requests": "/accounts/12345/close-requests"
191 }
192 }
193 }
194 ```
195
196 Here, the "hypermedia controls" are encoded in a `links` property on the account object.
197
198 Unfortunately, the client of this API still needs to know quite a bit of additional information:
199
200 * What http methods can be used against these URLs?
201 * Can it issue a `GET` to these URLs in order to get a representation of the mutation in question?
202 * If it can `POST` to a given URL, what values are expected?
203
204 Compare the above JSON with the following HTTP response, retrieved by a browser after a user has clicked on the
205 link to `/accounts/12345/deposits` found in the first HTML example:
206
207 ```html
208 HTTP/1.1 200 OK
209
210 <html>
211 <body>
212 <form method="post" action="/accounts/12345/deposits">
213 <input name="amount" type="number"/>
214 <button>Submit</button>
215 </form>
216 <body>
217 </html>
218 ```
219
220 Note that this HTML response encodes all the information necessary to update the account balance, providing a `form`
221 with a `method`
222 and `action` attribute, as well as the inputs necessary for updating the resource correctly.
223
224 The JSON representation does not have the same self-contained "uniform interface" as the HTML representation does.
225
226 Labelling JSON APIs, no matter how far they stray from RESTful concepts, as 'REST' has lead Roy Fielding to say:
227
228 > I am getting frustrated by the number of people calling any HTTP-based interface a REST API. Today’s example is the
229 > SocialSite REST API. That is RPC. It screams RPC. There is so much coupling on display that it should be given an X
230 > rating.
231
232 While attempts have been made to impose more elaborate hypermedia controls on JSON APIs, broadly the industry has
233 rejected
234 this approach in favor of simpler RPC-style APIs that forego HATEOAS and other elements of the REST-ful architecture.
235
236 This fact is strong evidence for the assertion that a natural hypermedia such as HTML is a practical
237 necessity for building RESTful systems.
238
239 <style>
240 .content {
241 font-family: 'Source Serif Pro', serif;
242 text-align: justify;
243 hyphens: auto;
244 margin-bottom: 3em;
245 }
246
247 .content h1 {
248 font-family: 'Lexend Zetta', Haettenschweiler, Impact, sans-serif;
249 margin: 16px;
250 font-size: min(10vw, 6em);
251 line-height: 1em;
252 margin-bottom: 5rem;
253 text-align: center;
254 }
255
256 .content section:after {
257 content: '< / >';
258 content: '< / >' / '';
259 display: block;
260 margin-bottom: 32px;
261 text-align: center;
262 color: #aaa;
263 font-weight: bold;
264 letter-spacing: .5em;
265 }
266
267 .content h2 {
268 font-size: 1em;
269 margin: 16px;
270 margin-top: 32px;
271 text-transform: uppercase;
272 letter-spacing: .1em;
273 text-align: center;
274 }
275 .content h2 em {
276 text-transform: none;
277 letter-spacing: 0;
278 }
279
280 .content a {
281 font-variant: all-small-caps;
282 letter-spacing: .08em;
283 font-weight: 600;
284 }
285
286 .content blockquote {
287 border: none;
288 font-style: italic;
289 font-size: 1.1em;
290 }
291 </style>