rest_principles.asciidoc 7.1 KB

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