| 1 |
[[rest_principles]] |
| 2 |
== REST principles |
| 3 |
|
| 4 |
This chapter will attempt to define the concepts behind REST |
| 5 |
and explain what makes a service RESTful. |
| 6 |
|
| 7 |
REST is often confused with performing a distinct operation |
| 8 |
depending on the HTTP method, while using more than the GET |
| 9 |
and POST methods. That's highly misguided at best. |
| 10 |
|
| 11 |
We will first attempt to define REST and will look at what |
| 12 |
it means in the context of HTTP and the Web. |
| 13 |
For a more in-depth explanation of REST, you can read |
| 14 |
http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm[Roy T. Fielding's dissertation] |
| 15 |
as it does a great job explaining where it comes from and |
| 16 |
what it achieves. |
| 17 |
|
| 18 |
=== REST architecture |
| 19 |
|
| 20 |
REST is a *client-server* architecture. The client and the server |
| 21 |
both have a different set of concerns. The server stores and/or |
| 22 |
manipulates information and makes it available to the user in |
| 23 |
an efficient manner. The client takes that information and |
| 24 |
displays it to the user and/or uses it to perform subsequent |
| 25 |
requests for information. This separation of concerns allows both |
| 26 |
the client and the server to evolve independently as it only |
| 27 |
requires that the interface stays the same. |
| 28 |
|
| 29 |
REST is *stateless*. That means the communication between the |
| 30 |
client and the server always contains all the information needed |
| 31 |
to perform the request. There is no session state in the server, |
| 32 |
it is kept entirely on the client's side. If access to a resource |
| 33 |
requires authentication, then the client needs to authenticate |
| 34 |
itself with every request. |
| 35 |
|
| 36 |
REST is *cacheable*. The client, the server and any intermediary |
| 37 |
components can all cache resources in order to improve performance. |
| 38 |
|
| 39 |
REST provides a *uniform interface* between components. This |
| 40 |
simplifies the architecture, as all components follow the same |
| 41 |
rules to speak to one another. It also makes it easier to understand |
| 42 |
the interactions between the different components of the system. |
| 43 |
A number of constraints are required to achieve this. They are |
| 44 |
covered in the rest of the chapter. |
| 45 |
|
| 46 |
REST is a *layered system*. Individual components cannot see |
| 47 |
beyond the immediate layer with which they are interacting. This |
| 48 |
means that a client connecting to an intermediate component, like |
| 49 |
a proxy, has no knowledge of what lies beyond. This allows |
| 50 |
components to be independent and thus easily replaceable or |
| 51 |
extendable. |
| 52 |
|
| 53 |
REST optionally provides *code on demand*. Code may be downloaded |
| 54 |
to extend client functionality. This is optional however because |
| 55 |
the client may not be able to download or run this code, and so |
| 56 |
a REST component cannot rely on it being executed. |
| 57 |
|
| 58 |
=== Resources and resource identifiers |
| 59 |
|
| 60 |
A resource is an abstract concept. In a REST system, any information |
| 61 |
that can be named may be a resource. This includes documents, images, |
| 62 |
a collection of resources and any other information. Any information |
| 63 |
that can be the target of an hypertext link can be a resource. |
| 64 |
|
| 65 |
A resource is a conceptual mapping to a set of entities. The set of |
| 66 |
entities evolves over time; a resource doesn't. For example, a resource |
| 67 |
can map to "users who have logged in this past month" and another |
| 68 |
to "all users". At some point in time they may map to the same set of |
| 69 |
entities, because all users logged in this past month. But they are |
| 70 |
still different resources. Similarly, if nobody logged in recently, |
| 71 |
then the first resource may map to the empty set. This resource exists |
| 72 |
regardless of the information it maps to. |
| 73 |
|
| 74 |
Resources are identified by uniform resource identifiers, also known |
| 75 |
as URIs. Sometimes internationalized resource identifiers, or IRIs, |
| 76 |
may also be used, but these can be directly translated into a URI. |
| 77 |
|
| 78 |
In practice we will identify two kinds of resources. Individual |
| 79 |
resources map to a set of one element, for example "user Joe". |
| 80 |
Collection of resources map to a set of 0 to N elements, |
| 81 |
for example "all users". |
| 82 |
|
| 83 |
=== Resource representations |
| 84 |
|
| 85 |
The representation of a resource is a sequence of bytes associated |
| 86 |
with metadata. |
| 87 |
|
| 88 |
The metadata comes as a list of key-value pairs, where the name |
| 89 |
corresponds to a standard that defines the value's structure and |
| 90 |
semantics. With HTTP, the metadata comes in the form of request |
| 91 |
or response headers. The headers' structure and semantics are well |
| 92 |
defined in the HTTP standard. Metadata includes representation |
| 93 |
metadata, resource metadata and control data. |
| 94 |
|
| 95 |
The representation metadata gives information about the |
| 96 |
representation, such as its media type, the date of last |
| 97 |
modification, or even a checksum. |
| 98 |
|
| 99 |
Resource metadata could be link to related resources or |
| 100 |
information about additional representations of the resource. |
| 101 |
|
| 102 |
Control data allows parameterizing the request or response. |
| 103 |
For example, we may only want the representation returned if |
| 104 |
it is more recent than the one we have in cache. Similarly, |
| 105 |
we may want to instruct the client about how it should cache |
| 106 |
the representation. This isn't restricted to caching. We may, |
| 107 |
for example, want to store a new representation of a resource |
| 108 |
only if it wasn't modified since we first retrieved it. |
| 109 |
|
| 110 |
The data format of a representation is also known as the media |
| 111 |
type. Some media types are intended for direct rendering to the |
| 112 |
user, while others are intended for automated processing. The |
| 113 |
media type is a key component of the REST architecture. |
| 114 |
|
| 115 |
=== Self-descriptive messages |
| 116 |
|
| 117 |
Messages must be self-descriptive. That means that the data |
| 118 |
format of a representation must always come with its media |
| 119 |
type (and similarly requesting a resource involves choosing |
| 120 |
the media type of the representation returned). If you are |
| 121 |
sending HTML, then you must say it is HTML by sending the |
| 122 |
media type with the representation. In HTTP this is done |
| 123 |
using the content-type header. |
| 124 |
|
| 125 |
The media type is often an IANA registered media type, like |
| 126 |
`text/html` or `image/png`, but does not need to be. Exactly |
| 127 |
two things are important for respecting this constraint: that |
| 128 |
the media type is well specified, and that the sender and |
| 129 |
recipient agree about what the media type refers to. |
| 130 |
|
| 131 |
This means that you can create your own media types, like |
| 132 |
`application/x-mine`, and that as long as you write the |
| 133 |
specifications for it and that both endpoints agree about |
| 134 |
it then the constraint is respected. |
| 135 |
|
| 136 |
=== Hypermedia as the engine of application state |
| 137 |
|
| 138 |
The last constraint is generally where services that claim |
| 139 |
to be RESTful fail. Interactions with a server must be |
| 140 |
entirely driven by hypermedia. The client does not need |
| 141 |
any prior knowledge of the service in order to use it, |
| 142 |
other than an entry point and of course basic understanding |
| 143 |
of the media type of the representations, at the very least |
| 144 |
enough to find and identify hyperlinks and link relations. |
| 145 |
|
| 146 |
To give a simple example, if your service only works with |
| 147 |
the `application/json` media type then this constraint |
| 148 |
cannot be respected (as there are no concept of links in |
| 149 |
JSON) and thus your service isn't RESTful. This is the case |
| 150 |
for the majority of self-proclaimed REST services. |
| 151 |
|
| 152 |
On the other hand if you create a JSON based media type |
| 153 |
that has a concept of links and link relations, then |
| 154 |
your service might be RESTful. |
| 155 |
|
| 156 |
Respecting this constraint means that the entirety of the |
| 157 |
service becomes self-discoverable, not only the resources |
| 158 |
in it, but also the operations you can perform on it. This |
| 159 |
makes clients very thin as there is no need to implement |
| 160 |
anything specific to the service to operate on it. |