Debian Codegraph @

www/src/content/essays/how-did-rest-come-to-mean-the-opposite-of-rest.md

raw symbols SCIP
1 ---
2 title: "How Did REST Come To Mean The Opposite of REST?"
3 description: "In this article, Carson Gross explores how the term REST (Representational State Transfer) evolved to mean nearly the opposite of its original definition in modern web development. It traces how REST, originally defined by Roy Fielding to describe the web's architecture of hypermedia-driven interactions, came to be widely misused as a term for JSON-based APIs that lack the key hypermedia constraints that define true REST architectural style."
4 created: 2022-07-18
5 modified: 2022-11-26
6 authors: ["Carson Gross"]
7 tags: ["foundations"]
8 ---
9
10 <style>
11 pre {
12 margin: 32px !important;
13 }
14 </style>
15
16 ## Tapping The Sign
17
18 <img src="/img/tap-the-sign.png" alt="You are wrong" style="width: 80%;margin-left:10%; margin-top: 16px;margin-bottom: 16px">
19
20 > I am getting frustrated by the number of people calling any HTTP-based interface a REST API. Today’s example is the
21 > SocialSite REST API. That is RPC. It screams RPC. There is so much coupling on display that it should be given an
22 > X rating.
23 >
24 > What needs to be done to make the REST architectural style clear on the notion that hypertext is a constraint? In
25 > other words, if the engine of application state (and hence the API) is not being driven by hypertext, then it cannot
26 > be RESTful and cannot be a REST API. Period. Is there some broken manual somewhere that needs to be fixed?
27 >
28 > _--Roy Fielding, Creator of the term REST_
29 >
30 >
31 _&nbsp;&nbsp;&nbsp;[REST APIs must be hypertext-driven](https://roy.gbiv.com/untangled/2008/rest-apis-must-be-hypertext-driven)_
32
33 [REST](https://en.wikipedia.org/wiki/Representational_state_transfer) must be the most broadly misused technical term
34 in computer programming history.
35
36 I can't think of anything else that comes close.
37
38 Today, when someone uses the term REST, they are nearly always discussing a JSON-based API using HTTP.
39
40 When you see a job post mentioning REST or a company
41 discussing [REST Guidelines](https://github.com/microsoft/api-guidelines/blob/vNext/Guidelines)
42 they will rarely mention either hypertext or hypermedia: they will instead mention JSON, GraphQL(!) and the like.
43
44 Only a few obstinate folks grumble: but these JSON APIs aren't RESTful!
45
46 <iframe src="https://www.youtube.com/embed/HOK6mE7sdvs" title="Doesn't anyone notice this?"
47 frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope"
48 style="width: 400px;height:300px;margin-left:15%;margin-top: 16px;margin-bottom: 16px">
49 </iframe>
50
51 In this post, I'd like to give you
52 a [brief, incomplete and mostly wrong](https://james-iry.blogspot.com/2009/05/brief-incomplete-and-mostly-wrong.html)
53 history of REST, and how we got to a place where its meaning has been nearly perfectly inverted to mean what REST was
54 originally contrasted with: RPC.
55
56 ## Where Did REST Come From?
57
58 The term REST, short for REpresentational State Transfer, came from
59 [Chapter 5 of Fielding's PhD Dissertation](https://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm).
60 Fielding was describing the network architecture of the (then new) world wide web, and contrasting it with other
61 possible
62 network architectures, particularly RPC-style network architectures.
63
64 It is important to understand that, at the time of his writing (1999-2000), there were no JSON APIs: he was describing
65 the web as it existed at that time, with HTML being exchanged over HTTP as people "surfed the web". JSON hadn't been
66 created yet, and broad adoption of JSON was a decade off.
67
68 REST described a _network architecture_, and it was defined in terms of _constraints_ on an API, constraints that
69 needed to be met in order to be considered a RESTful API. The language is academic, which has contributed to the
70 confusion around the topic, but it is clear enough that most developers should be able to understand it.
71
72 ### The Crux of REST: The Uniform Interface & HATEOAS
73
74 REST has many constraints and concepts within it, but there is one crucial idea that I believe is the defining and
75 most distinguishing characteristic of REST, when contrasted with other possible network architectures.
76
77 This is known as
78 the [uniform interface constraint](https://en.wikipedia.org/wiki/Representational_state_transfer#Uniform_interface),
79 and more specifically within that concept, the idea
80 of [Hypermedia As The Engine of Application State (HATEOAS)](https://htmx.org/essays/hateoas/)
81 or as Fielding prefers to call it, the hypermedia constraint.
82
83 In order to understand this uniform interface constraint, lets consider two HTTP responses returning information about a
84 bank account, the first in HTML (a hypertext) and the second in JSON:
85
86 #### An HTML Response
87
88 ```html
89 HTTP/1.1 200 OK
90
91 <html>
92 <body>
93 <div>Account number: 12345</div>
94 <div>Balance: $100.00 USD</div>
95 <div>Links:
96 <a href="/accounts/12345/deposits">deposits</a>
97 <a href="/accounts/12345/withdrawals">withdrawals</a>
98 <a href="/accounts/12345/transfers">transfers</a>
99 <a href="/accounts/12345/close-requests">close-requests</a>
100 </div>
101 <body>
102 </html>
103 ```
104
105 #### A JSON Response
106
107 ```json
108 HTTP/1.1 200 OK
109
110 {
111 "account_number": 12345,
112 "balance": {
113 "currency": "usd",
114 "value": 100.00
115 },
116 "status": "good"
117 }
118 ```
119
120 The crucial difference between these two responses, and why the _HTML response_ is RESTful, but the
121 _JSON response_ is not, is this:
122
123 <p style="margin:32px;text-align: center;font-weight: bold">The HTML response is entirely self-describing.</p>
124
125 A proper hypermedia client that receives this response does not know what a bank account is, what a
126 balance is, etc. It simply knows how to render a hypermedia, HTML.
127
128 The client knows nothing about the API end points associated with this data, except via URLs and hypermedia controls
129 (links and forms) discoverable within the HTML itself. If the state of the resource changes such that the allowable
130 actions available on that resource change (for example, if the account goes into overdraft) then the HTML response would
131 change to show the new set of actions available.
132
133 The client would render this new HTML, totally unaware of what "overdraft" means or, indeed, even what a bank account
134 is.
135
136 It is in this manner that hypertext is the engine of application state: the HTML response "carries along" all the API
137 information necessary to continue interacting with the system directly within itself.
138
139 Now, contrast that with the second JSON response.
140
141 In this case the message is _not_ self describing. Rather, the client must know how to interpret the `status` field to
142 display an appropriate user interface. Further, the client must know what actions are available on the account based on
143 "out-of-band" information, that is, information on the URLs, parameters and so forth, derived from another source of
144 information _outside of the response_, such as swagger API documentation.
145
146 The JSON response is not self-describing and does not encode the state of the resource within a hypermedia. It therefore
147 fails the uniform interface constraint of REST, and, thus, is not RESTful.
148
149 ### Inventor: RESTful APIs Must Be Hypermedia Driven
150
151 In [Rest APIs Must Be Hypermedia Driven](https://roy.gbiv.com/untangled/2008/rest-apis-must-be-hypertext-driven),
152 Fielding
153 goes on to say:
154
155 > A REST API should be entered with no prior knowledge beyond the initial URI (bookmark) and set of standardized media
156 > types that are appropriate for the intended audience (i.e., expected to be understood by any client that might use the
157 > API). From that point on, all application state transitions must be driven by client selection of server-provided
158 > choices that are present in the received representations or implied by the user’s manipulation of those
159 > representations.
160
161 So, in a RESTful system, you should be able to enter the system through a single URL and, from that point on, all
162 navigation
163 and actions taken within the system should be entirely provided through self-describing hypermedia: through links and
164 forms in HTML, for example. Beyond the entry point, in a proper RESTful system, the API client shouldn't need any
165 additional information about your API.
166
167 This is the source of the incredible flexibility of RESTful systems: since all responses are self describing and
168 encode all the currently available actions available there is no need to worry about, for example, versioning your API!
169 In fact, you don't even need to document it!
170
171 If things change, the hypermedia responses change, and that's it.
172
173 It's an incredibly flexible and innovative concept for building distributed systems.
174
175 ### Industry: Lol, No, RESTful APIs Are JSON
176
177 Today, most web developers and most companies would call the _second example_ a RESTful API.
178
179 They probably wouldn't even regard the first response _as an API response_. It's just HTML!
180
181 (Poor HTML, can't get no respect.)
182
183 APIs are always JSON or maybe, if you are fancy, something like Protobuf, right?
184
185 <img src="/img/you-are-wrong.png" alt="You are wrong" style="width: 80%;margin-left:10%; margin-top: 16px;margin-bottom: 16px">
186
187 Wrong.
188
189 You are all wrong and you should feel bad.
190
191 The first response _is_ an API response, and, in fact, the one that is RESTful!
192
193 The second response is, in fact, a _Remote Procedure Call_ (RPC) style of API. The client and the server are coupled,
194 just like the SocialSite API Fielding complained about back in 2008: a client needs to have additional knowledge about
195 the resource it is working with that must be derived from some other source beyond the JSON response itself.
196
197 This API is, in spirit, nearly the opposite of REST.
198
199 Let's call this style of API "RESTless".
200
201 ## How "REST" came to mean "RESTless"
202
203 Now, how on earth did we get to a spot where APIs that are _obviously_ not RESTful are called RESTful by 99.9% of the
204 industry?
205
206 It's a funny story:
207
208 Roy Fielding published his dissertation in 2000.
209
210 Around the same time, [XML-RPC](https://en.wikipedia.org/wiki/XML-RPC), an explicitly RPC-inspired protocol was released
211 and started to gather steam as a method to build APIs using HTTP. XML-RPC was part of a larger project called
212 [SOAP](https://en.wikipedia.org/wiki/SOAP), from Microsoft. XML-RPC came out of a long tradition of RPC-style
213 protocols, mainly from the enterprise world, with a lot of static typing and early XML-maximalism thrown in as well.
214
215 Also arriving at this moment was [AJAX](https://en.wikipedia.org/wiki/Ajax_(programming)), or Asynchronous
216 JavaScript and XML. Note well the XML here. AJAX, as everyone now knows, allows browsers to issue HTTP requests
217 to the server in the background and process the response directly in JavaScript, opening up a whole new world of
218 programming for the web.
219
220 The question was: what should those requests look like? They were obviously going to be XML. Look, it's right there
221 in the name. And this new SOAP/XML-RPC standard was out, maybe that was the right thing?
222
223 ### Maybe REST can work for Web Services?
224
225 Some people noticed that the web had this different sort of architecture that Fielding had described, and began to ask
226 if REST, rather than SOAP, should be the preferred mechanism for accessing what were coming to be called "Web Services".
227 The web was proving to be extremely flexible and growing gang busters, so maybe the same network architecture, REST,
228 that
229 was working so well for browsers & humans would work well for APIs.
230
231 It sounded plausible, especially when XML was the format for APIs: XML sure _looks_ an awful lot like HTML, doesn't it?
232 You can imagine an XML API satisfying all of the RESTful constraints, up to and including the uniform interface.
233
234 So people began exploring this route as well.
235
236 While all this was happening, another important technology was in the process of being
237 born: [JSON](https://www.json.org/json-en.html)
238
239 JSON was (literally) JavaScript to SOAP/RPC-XML's Java: simple, dynamic and easy. It's hard to believe now,
240 when JSON is the dominant format for most web APIs, but it actually took a while for JSON to catch on. As late as 2008,
241 discussions around API development were mainly around XML, not JSON.
242
243 ### Formalizing REST APIs
244
245 In 2008, Martin Fowler published an article popularizing
246 the [Richardson Maturity Model](https://martinfowler.com/articles/richardsonMaturityModel.html),
247 a model to determine how RESTful a given API was.
248
249 The model proposed four "levels", with the first level being Plain Old XML, or The Swamp of POX.
250
251 <img src="/img/rmm.png" alt="Richardson Maturity Model" style="width: 80%;margin-left:10%; margin-top: 16px;margin-bottom: 16px">
252
253 From there, an API could be considered more "mature" as a REST API as it adopted the following ideas:
254
255 * Level 1: Resources (e.g. a resource-aware URL layout, contrasted with an opaque URL layout as in XML-RPC)
256 * Level 2: HTTP Verbs (using `GET`, `POST`, `DELETE`, etc. properly)
257 * Level 3: Hypermedia Controls (e.g. links)
258
259 Level 3 is where the uniform interface comes in, which is why this level is considered the most mature and truly "The
260 Glory of REST"
261
262 ### "REST" Wins, Kinda...
263
264 Unfortunately for the term REST, two things happened at this time:
265
266 * Everyone switched to JSON
267 * Everyone stopped at Level 2 of the RMM
268
269 JSON rapidly took over the web service/API world because SOAP/XML-RPC was so dramatically over-engineered. JSON was
270 simple,
271 "just worked" and was easy to read and understand.
272
273 With this change, the web development world threw off the shackles of the
274 [J2EE mindset](https://en.wikipedia.org/wiki/Jakarta_EE) conclusively, relegating SOAP/XML-RPC to an enterprise-only
275 affair.
276
277 Since the REST approach wasn't as tied to XML as SOAP/XML-RPC was, and since it didn't impose as much formality on
278 end points, REST was the natural place for JSON to take over. And it did so, rapidly.
279
280 During this crucial change, something became increasingly clear: most JSON APIs were stopping at Level 2 of the RMM.
281
282 Some pushed through to Level 3 by incorporating hypermedia controls in their responses, but nearly all these APIs still
283 needed to publish documentation, indicating that the "Glory of REST" was not being achieved.
284
285 JSON taking over as the response format should have been a strong hint as well: JSON is obviously not a hypertext. You
286 can impose hypermedia controls on top of it, but it isn't natural. XML at least _looked_ like HTML, kinda, so it was
287 plausible that you could create a hypermedia with it.
288
289 JSON was just... data. Adding hypermedia controls was awkward, non-standardized and rarely used in the manner described
290 by the uniform interface constraint.
291
292 Despite these difficulties, the term REST stuck: REST was the opposite of SOAP, JSON APIs weren't SOAP, therefore
293 JSON APIs were REST.
294
295 That's the one sentence version of how we got here.
296
297 ### The REST Wars
298
299 Despite the JSON API world never consistently achieving truly RESTful APIs, there were plenty of fights over whether
300 or not the RESTless APIs being created were "RESTful": arguments over URL layouts, over which HTTP verb was
301 appropriate for a given action, flame wars about media types, and so forth.
302
303 I was young at the time, and the whole thing struck me as opaque, puritanical and alienating, so I pretty much gave up
304 on the whole idea of REST: it was something condescending people fought about on the internet.
305
306 What I rarely saw mentioned (or, when I did, what I didn't understand) was the concept of the uniform interface and how
307 crucial it is to a RESTful system.
308
309 It wasn't until I created [intercooler.js](https://intercoolerjs.org) and a few smart folks started telling me that it
310 was
311 RESTful that I got interested in the idea again.
312
313 RESTful? That's a JSON API thing, how could my hack of a front-end library be RESTful?
314
315 So I looked into it, reread Fielding's dissertation with fresh eyes, and discovered, lo and behold, not only was
316 intercooler RESTful, but all the "RESTful" JSON APIs I was dealing with weren't RESTful at all!
317
318 And, with that, I began boring the internet to tears:
319
320 * [Rescuing REST From the API Winter](https://intercoolerjs.org/2016/01/18/rescuing-rest.html)
321 * [The API Churn/Security Trade-off](https://intercoolerjs.org/2016/02/17/api-churn-vs-security.html)
322 * [HATEOAS is for Humans](https://intercoolerjs.org/2016/05/08/hatoeas-is-for-humans.html)
323 * [Taking HTML Seriously](https://intercoolerjs.org/2020/01/14/taking-html-seriously)
324 * [Hypermedia APIs vs. Data APIs](/essays/hypermedia-apis-vs-data-apis)
325 * [HATEOAS](/essays/hateoas)
326 * [Hypermedia Driven Applications](/essays/hypermedia-driven-applications)
327 * This, gentle reader, [your current article](/essays/how-did-rest-come-to-mean-the-opposite-of-rest).
328
329 ### The State of REST Today
330
331 Eventually most people got tired of trying to add hypermedia controls to JSON APIs and gave up on it. While these
332 controls
333 worked well in certain specialized situations (e.g. paging), they never achieved the broad, obvious utility
334 that REST found in the general, human-oriented
335 internet. [(I have a theory why that is.)](https://intercoolerjs.org/2016/05/08/hatoeas-is-for-humans.html)
336
337 Things settled into this intermediate RESTless state, with REST slowly cementing its meaning as a JSON API at Level 1
338 or 2 of the RMM. But there was always the possibility that we would break through to Level 3 and the glory of REST.
339
340 Then Single Page Applications (SPAs) hit.
341
342 When SPAs hit, web development became disconnected entirely from the original underlying RESTful architecture. The
343 *entire
344 networking architecture* of SPA applications moved over to the JSON RPC style. Additionally, due to the complexity of
345 these
346 applications, developers specialized into front end and back end.
347
348 The front end developers were obviously _not_ doing anything RESTful: they were working with JavaScript, building DOM
349 object, and calling AJAX APIs when needed. This was much more like thick-client authoring than anything like the
350 early web.
351
352 The back end engineers were still concerned with the network architecture to an extent, and they continued to use the
353 term "REST" to describe what they were doing.
354
355 Even though they were doing things like publishing swagger documentation for their RESTful APIs
356 or [complaining about API
357 churn of their RESTful APIs](https://www.infoq.com/articles/no-more-mvc-frameworks/), things that wouldn't be occurring
358 if they were actually creating RESTful APIs.
359
360 Finally, in the late 2010s, people had had enough: REST, even in its RESTless form, simply wasn't keep up with the
361 needs
362 of increasingly complex SPA applications. The applications were becoming more and more like thick clients, and thick
363 client problems need thick client solutions, not bastardized hypermedia client solutions.
364
365 The dam really broke when [GraphQL](https://en.wikipedia.org/wiki/GraphQL) was released.
366
367 GraphQL couldn't be less RESTful: you absolutely *have to have* documentation to understand how to work with an API
368 that uses GraphQL. The client and the server are extremely tightly coupled. There are no native hypermedia controls
369 in it. It offers schemas and, in many ways, feels a lot like an updated and stripped-down version of XML-RPC.
370
371 And here I want to say: that's OK!
372
373 People really, really like GraphQL in many cases and, if you are building a thick client style application, that makes a
374 lot of sense:
375
376 > The short answer to this question is that HATEOAS isn’t a good fit for most modern use cases for APIs. That is why
377 > after
378 > almost 20 years, HATEOAS still hasn’t gained wide adoption among developers. GraphQL on the other hand is spreading
379 > like wildfire because it solves real-world problems.
380 >
381 >
382 _[GraphQL and REST Level 3 (HATEOAS)](https://techblog.commercetools.com/graphql-and-rest-level-3-hateoas-70904ff1f9cf)_
383
384 So GraphQL isn't REST, it doesn't claim to be REST, it doesn't want to be REST.
385
386 But, as of today, the vast majority of developers and companies, even as they excitedly add GraphQL functionality to
387 their APIs, continue to use the term REST to describe what they are building.
388
389 ## OK, What Can We Do About This Situation?
390
391 Unfortunately, [voidfunc](https://news.ycombinator.com/item?id=32073545) is probably right:
392
393 > You can tap the sign as much as you want, that battle was lost a long time ago. REST is just the common term people
394 > use for HTTP+JSON RPC.
395
396 We are going to keep calling _obviously_ non-RESTful JSON APIs REST because that's just what everyone calls them now.
397
398 Despite my increasingly vigorous sign tapping, 50 years from now Global Omni Corp. will still be advertising
399 jobs for working on v138 of their RESTful JSON API's swagger documentation.
400
401 <img src="/img/punished-fielding.png" alt="Roy Fielding Does Not Approve" style="width: 80%;margin-left:10%; margin-top: 16px;margin-bottom: 16px">
402
403 [The situation is hopeless, but not serious.](https://wwnorton.com/books/9780393310214)
404
405 Regardless, there is an opportunity here to explain REST and, in particular, the uniform interface to a new generation
406 of web
407 developers who may have never heard of those concepts in their original context, and who assume REST === JSON APIs.
408
409 [People sense something is wrong](/essays/a-response-to-rich-harris), and maybe REST, real, actual REST,
410 not RESTless, could be a part of [the answer to that](/essays/spa-alternative).
411
412 At the very least, the ideas behind REST are interesting and worth knowing just as general software engineering
413 knowledge.
414
415 There is a larger meta-point here too: even a relatively smart group of people (early web developers), with the benefit
416 of the internet, and with a pretty clear (if at times academic) specification for the term REST, were unable to keep the
417 meaning consistent with its original meaning over period of two decades.
418
419 If we can get this so obviously wrong, what else could we be wrong about?