Debian Codegraph @

www/src/content/essays/rest-explained.md

raw symbols SCIP
1 ---
2 title: "REST - Explained For Beginners"
3 description: "In this essay, Carson Gross presents the core concepts of REST (Representational State Transfer) explained in simple terms for beginners. He breaks down Roy Fielding's dissertation to highlight key principles like the Uniform Interface, Statelessness, Hypermedia (HATEOAS), and more, making REST easy to understand for non-academic web developers."
4 created: 2021-07-13
5 modified: 2022-02-06
6 authors: ["Carson Gross"]
7 tags: ["foundations"]
8 ---
9
10 There is no topic that generates more confusion in web development than the idea of Representational State Transfer,
11 known as REST. This term comes from [Chapter 5](https://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm)
12 of [Roy Fielding's](https://en.wikipedia.org/wiki/Roy_Fielding) PhD thesis at [U.C. Irvine](https://www.uci.edu/).
13
14 In this essay we will go through this Chapter and summarize the important concepts for non-academic web developers. The
15 thesis is dense and involves a lot of technical jargon that isn't relevant to people who aren't academics interested
16 in formal PhD thesis writing.
17
18 By the end of this essay you should have a better handle on REST, and the concept of a
19 [Uniform Interface](https://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm#sec_5_1_5) in particular.
20
21 ## Overview
22
23 The first thing to understand about REST is that *it is a description of the original web*. Fielding describes REST as
24 an
25 "architectural style for distributed hypermedia systems", which sounds fancy but just means the web we all know and
26 love:
27 clicking on hyperlinks, submitting forms, looking at images, reading paragraphs and all that jazz.
28
29 It was *NOT* created as a description of a particular approach for JSON APIs, although that is the context
30 that most people hear about REST today in. Fielding was describing the early web and, in particular, how it was
31 different
32 from earlier client/server architectures.
33
34 ## Section 5.1 Deriving Rest
35
36 In [section 5.1](https://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm#sec_5_1), unfortunately for
37 non-academics, Fielding adopts the technique of deriving REST from first principles. Here I will summarize each section
38 and clarify and add context in the important ones.
39
40 ### [Client Server](https://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm#sec_5_1_2)
41
42 REST is, of course, a client-server architecture, since the web is a client (browser) server (http server) system.
43
44 ### [Stateless](https://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm#sec_5_1_3)
45
46 The web, most developers know, is intended to be stateless. All requests should encapsulate all information necessary
47 to understand that request. For example, there should not be a long running transaction implicitly associated with a
48 series
49 of requests, as you might have with a SQL database session.
50
51 ### [Cache](https://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm#sec_5_1_4)
52
53 HTTP, you probably know, has a [caching mechanism](https://developer.mozilla.org/en-US/docs/Web/HTTP/Caching) built into
54 it. You don't need to know the details of this now, but may explore it later.
55
56 ### [Uniform Interface](https://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm#sec_5_1_5)
57
58 This section, in my mind, is the crux of the REST architecture and, unfortunately, is very brief, so we will spend some
59 time expanding on it, rather that just summarizing it. The chapter begins:
60
61 > The central feature that distinguishes the REST architectural style from other network-based styles
62 > is its emphasis on a uniform interface between components
63
64 To clarify the discussion around exactly what the uniform interface is, let's consider some simple HTML that I hope
65 everyone reading this will understand:
66
67 ```html
68 <html>
69 <body>
70 <section>
71 <p>
72 Name: Joe Blow
73 </p>
74 <p>
75 Email: [email protected]
76 </p>
77 <p>
78 <a href="/contacts/42/edit">Edit</a>
79 <a href="/contacts/42/email">Email</a>
80 <a href="/contacts/42/archive">Archive</a>
81 </p>
82 </section>
83 </body>
84 </html>
85 ```
86
87 Here we have a basic bit of html, with some divs, a bit of information and then some anchor tags to perform various
88 operations on a contact. Nothing fancy. Again, for the discussion, imagine this content could be found at
89 `http://example.com/contacts/42`.
90
91 Back to the dissertation:
92
93 > REST is defined by four interface constraints: identification of resources; manipulation of resources through
94 > representations; self-descriptive messages; and, hypermedia as the engine of application state.
95
96 Let's go through each of these in turn.
97
98 #### Identification of Resources
99
100 The first aspect of Rest is the idea of *resources* that are found somewhere via...
101 well, [Universal Resource Locators](https://developer.mozilla.org/en-US/docs/Learn/Common_questions/What_is_a_URL), or
102 URLs. Note that the HTML contains additional URLs for the actions that you can perform on this
103 resource (`contacts/42`), following the conventional hierarchical arrangement of URL paths.
104
105 #### Manipulation of Resources Through Representations
106
107 This sounds fancy, but it just means that you can update and mutate the resource (that is, the contact) through various
108 representations (that is HTML pages) rather than having to issues, say, SQL, to modify it.
109
110 #### Self Descriptive Messages
111
112 This is a key concept of REST. Note that the browser, which is the client in this client-server setup, *knows nothing
113 about contacts*. And yet it is able to render a "Contact UI" simply by rendering the HTML returned by the server. The
114 message itself is entirely self-describing, containing all information the client needs about both the data and the
115 possible
116 operations on that data (in the form of links.)
117
118 Now, contrast this with a JSON representation of the same data:
119
120 ```json
121 {
122 "name" : "Joe Blow",
123 "email" : "[email protected]"
124 }
125 ```
126
127 Obviously this is smaller, but a client working with this data must decide two crucial things:
128
129 * How to render it
130 * What actions are available to mutate it
131
132 The first part is typically done with a client side template. The second is typically done by reading the documentation
133 for the API and encoding the interactions with the server directly in the client.
134
135 This is the crux of the difference between REST-ful systems and traditional client-server system: in the REST-ful system
136 the client (i.e. the browser) doesn't know anything about the resource, it just knows how to render a hypermedia. In
137 the client-server system, knowledge about the resource is embedded in the client.
138
139 There are pros and cons to both approaches, but the REST-ful approach, in the form of the early web, proved to be
140 extremely reliable and flexible. It hides a tremendous amount of knowledge about the resources behind this *uniform
141 interface* of HTML, so the client doesn't have the opportunity to break in the way the thick-client does.
142
143 Now, you may have noticed that, in the last decade, web development has trended away from the REST-ful architecture
144 and towards a more traditional client-server setup, using JSON APIs. And you may have noticed a lot more discussion and
145 issues around versioning APIs, providing more general query functionality and so on. This is not accidental: we are
146 losing the flexibility of the REST-ful model as we turn the browser into a VM for hosting thick client applications.
147
148 #### Hypermedia As The Engine of Application State (HATEOAS)
149
150 This last concept dovetails with the previous one: clients transition application state by interacting with URLs
151 found in the hypermedia itself (via forms and links). So, in the HTML example above, the ability to edit, email
152 and archive the contact all encoded as anchors in the HTML. If one of those actions was not available, or a new
153 one became available, it would come down in a new bit of HTML, after a page refresh.
154
155 This is in contrast with a thick client approach where, for example, a local store may be sync'd asynchronously with
156 a back end and, thus, the HTML is not acting as the engine of application state, but rather as a (somewhat janky)
157 UI description language.
158
159 Somewhat hilariously, the [Wikipedia article on HATEOAS](https://en.wikipedia.org/wiki/HATEOAS) uses JSON, which is not
160 a natural hypermedia. You can layer some REST-ful behavior on top of JSON if you want, but it has rarely been useful
161 in the real world, and HATEOAS is usually ignored in JSON APIs. This makes sense because JSON APIs are useful mainly
162 for the traditional client-server architecture and aren't particularly amenable to the REST-ful style.
163
164 #### Uniform Interface Summary
165
166 That's the crux of REST and really the crux of this essay. You can read on for a bit more detail and analysis of
167 Fieldings
168 paper, but the core take away here is that there is a sharp distinction between a REST-ful hypermedia architecture and
169 traditional client-server architectures, and that distinction revolves mainly around the concept of a uniform interface,
170 and the self-describing nature of them in particular.
171
172 Again, don't get bogged down in the jargon here, just think about this HTML and what a miracle of flexibility and
173 ingenuity it is:
174
175 ```html
176 <html>
177 <body>
178 <div>
179 <div>
180 Name: Joe Blow
181 </div>
182 <div>
183 Email: [email protected]
184 </div>
185 <div>
186 <a href="/contacts/42/edit">Edit</a>
187 <a href="/contacts/42/email">Email</a>
188 <a href="/contacts/42/archive">Archive</a>
189 </div>
190 </div>
191 </body>
192 </html>
193 ```
194
195 ### [Layered System](https://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm#sec_5_1_6)
196
197 You don't need to know much about this, except
198 that [CDNs exist](https://en.wikipedia.org/wiki/Content_delivery_network), and you should use them.
199
200 ### [Code-On-Demand](https://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm#sec_5_1_7)
201
202 Again, you don't need to know much about this, except
203 that [Javascript exists](https://developer.mozilla.org/en-US/docs/Web/javascript), and
204 that it's the only part that's optional.
205
206 ## Section 5.2 - [REST Architectural Elements](https://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm#sec_5_2)
207
208 I won't drill in as deeply on this section as we did others because it gets pretty technical and, frankly, is a bit
209 boring and repetitive (as one might expect from a dissertation.) The two big ideas in this section are Resources and
210 Representations.
211
212 ## Section 5.2.1 - [Resources and Resource Identifiers](https://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm#sec_5_2)
213
214 From the paper:
215
216 > The key abstraction of information in REST is a resource. Any information that can be named can be a resource: a
217 > document
218 > or image, a temporal service (e.g. "today's weather in Los Angeles"), a collection of other resources, a
219 > non-virtual object (e.g. a person), and so on.
220
221 Practically, a resource is anything that can be addressed by a URL. What happens when you access a URL?
222
223 Well, you get back a *representation* of that resource, in the form of an HTTP response that may contain HTML,
224 directives
225 and so forth.
226
227 ## Section 5.2.1 - [Representations](https://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm#sec_5_2)
228
229 I don't find a lot of practical use in this section. There is some stuff on control data, media types and so forth,
230 which are all worth learning about eventually when needed, but aren't a commonly used aspect of web development.
231
232 The remaining sections 5.2 similarly do not offer much to the generalist.
233
234 ## Section 5.3 - [REST Architectural Views](https://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm#sec_5_3)
235
236 In what is becoming a pattern, I again do not feel there is a lot of useful new information for the average web
237 developer in this section, with one big exception: it lays out the benefits of REST.
238
239 From the paper:
240
241 > REST's client-server separation of concerns simplifies component implementation, reduces the complexity of connector
242 > semantics,
243 > improves the effectiveness of performance tuning, and increases the scalability of pure server components.
244 > Layered system constraints allow intermediaries--proxies, gateways, and firewalls--to be introduced at various points
245 > in the communication without changing the interfaces between components, thus allowing them to assist in communication
246 > translation or improve performance via large-scale, shared caching. REST enables intermediate processing by
247 > constraining
248 > messages to be self-descriptive: interaction is stateless between requests, standard methods and media types are used
249 > to indicate semantics and exchange information, and responses explicitly indicate cacheability.
250
251 This is all very true, and is why the web has been so successful and will continue to be successful.
252
253 ## [Sections 5.4](https://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm#sec_5_4) & [5.5](https://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm#sec_5_5) - Related Work & Summary
254
255 These brief sections are not relevant to non-academics interested in REST.
256
257 ## Summary
258
259 So there you have it, a brief tour of Chapter 5 of Roy Fielding's dissertation, which gave us the term REST. I have
260 focused in on the areas that I think are most important for web developers to understand and tried to convey how
261 REST describes the original web model. The uniform interface concept is, in my opinion, the most important and
262 interesting
263 aspect of REST, and is useful for web developers to understand as it is primarily responsible for the benefits described
264 above.
265
266 Finally, I hope you can see how inappropriate REST is for describing most JSON APIs in use today.