Representational State Transfer (REST)

Web Architecture [./]
Fall 2011 — INFO 253 (CCN 42598)

Dilan Mahendran, UC Berkeley School of Information
2011-11-10

Creative Commons License [http://creativecommons.org/licenses/by/3.0/]

This work is licensed under a CC
Attribution 3.0 Unported License
[http://creativecommons.org/licenses/by/3.0/]

Contents D. Mahendran: Representational State Transfer (REST)

Contents

D. Mahendran: Representational State Transfer (REST)

(2) Abstract

Representational State Transfer (REST) is an architectural style for building distributed systems. The Web is an example for such a system. REST-style applications can be built using a wide variety of technologies. REST's main principles are those of resource-oriented states and functionalities, the idea of a unique way of identifying resources, and the idea of how operations on these resources are defined in terms of a single protocol for interacting with resources. REST-oriented system design leads to systems which are open, scalable, extensible, and easy to understand.



Abstraction Layers

Outline (Abstraction Layers)

  1. Abstraction Layers [3]
  2. REST: The Definition [6]
  3. Web Architecture [6]
    1. Uniform Resource Identifier (URI) [2]
    2. Hypertext Transfer Protocol (HTTP) [3]
  4. Representations [7]
    1. Structured Documents [5]
    2. Linked Documents [2]
  5. State [6]
Abstraction Layers D. Mahendran: Representational State Transfer (REST)

(4) What is REST?

  1. An architectural style for building loosely coupled systems
    • defined by a set of general constraints (principles)
    • the Web (URI/HTTP/HTML/XML) is an instance of this style
  2. The Web used correctly (i.e., not using the Web as transport)
    • HTTP is built according to RESTful principles
    • services are built on top of Web standards without misusing them
    • most importantly, HTTP is an application protocol (not a transport protocol)
  3. Anything that uses HTTP and XML (XML without SOAP)
    • XML-RPC was the first approach for this
    • violates REST because there is no uniform interface


Abstraction Layers D. Mahendran: Representational State Transfer (REST)

(5) Architectural Styles

Louvre Interior
  • Architecture: Louvre [http://en.wikipedia.org/wiki/Louvre]
  • Architectural Style: Baroque [http://en.wikipedia.org/wiki/Baroque_architecture]
Villa Savoye Interior
  • Architecture: Villa Savoye [http://en.wikipedia.org/wiki/Villa_Savoye]
  • Architectural Style: International Style [http://en.wikipedia.org/wiki/International_Style_(architecture)]


Abstraction Layers D. Mahendran: Representational State Transfer (REST)

(6) REST is not an Architecture



REST: The Definition

Outline (REST: The Definition)

  1. Abstraction Layers [3]
  2. REST: The Definition [6]
  3. Web Architecture [6]
    1. Uniform Resource Identifier (URI) [2]
    2. Hypertext Transfer Protocol (HTTP) [3]
  4. Representations [7]
    1. Structured Documents [5]
    2. Linked Documents [2]
  5. State [6]
REST: The Definition D. Mahendran: Representational State Transfer (REST)

(8) The REST Architectural Style

  1. Resource Identification [Resource Identification (1)]
  2. Uniform Interface [Uniform Interface (1)]
  3. Self-Describing Messages [Self-Describing Messages (1)]
  4. Hypermedia Driving Application State [Hypermedia Driving Application State (1)]
  5. Stateless Interactions [Stateless Interactions (1)]


REST: The Definition D. Mahendran: Representational State Transfer (REST)

(9) Resource Identification



REST: The Definition D. Mahendran: Representational State Transfer (REST)

(10) Uniform Interface



REST: The Definition D. Mahendran: Representational State Transfer (REST)

(11) Self-Describing Messages



REST: The Definition D. Mahendran: Representational State Transfer (REST)

(12) Hypermedia Driving Application State



REST: The Definition D. Mahendran: Representational State Transfer (REST)

(13) Stateless Interactions



Web Architecture

Outline (Web Architecture)

  1. Abstraction Layers [3]
  2. REST: The Definition [6]
  3. Web Architecture [6]
    1. Uniform Resource Identifier (URI) [2]
    2. Hypertext Transfer Protocol (HTTP) [3]
  4. Representations [7]
    1. Structured Documents [5]
    2. Linked Documents [2]
  5. State [6]
Web Architecture D. Mahendran: Representational State Transfer (REST)

(15) What is the Web?



Uniform Resource Identifier (URI)

Outline (Uniform Resource Identifier (URI))

  1. Abstraction Layers [3]
  2. REST: The Definition [6]
  3. Web Architecture [6]
    1. Uniform Resource Identifier (URI) [2]
    2. Hypertext Transfer Protocol (HTTP) [3]
  4. Representations [7]
    1. Structured Documents [5]
    2. Linked Documents [2]
  5. State [6]
Uniform Resource Identifier (URI) D. Mahendran: Representational State Transfer (REST)

(17) Identifying Resources on the Web

  • Essential for implementing a Resource Identification [Resource Identification (1)]
  • URIs [URIs & HTTP; Uniform Resource Identifier (URI) (1)] are human-readable universal identifiers for stuff
    • many identification schemes are not human-readable (binary or hex strings)
    • many RPC-based systems do not have universally identified objects
  • Making every thing a universally unique identified thing is important
    • it removes the necessity to scope non-universal identifiers
    • it allows to talk about all things in exactly the same way
  • URI-identified things should have well-defined interactions


Uniform Resource Identifier (URI) D. Mahendran: Representational State Transfer (REST)

(18) Processing URIs

  • Processing URIs is not as trivial as it may seem
    • escaping and normalization rules are non-trivial
    • many implementations are broken
    • complain about broken implementations
    • even more complicated when processing an Internationalized Resource Identifier (IRI)
  • URIs are not just strings
    • URIs are strings with a considerable set of rules attached to them
    • implementing all these rules is non-trivial
    • implementing all these rules is crucial
    • application development environments provide functions for URI handling


Hypertext Transfer Protocol (HTTP)

Outline (Hypertext Transfer Protocol (HTTP))

  1. Abstraction Layers [3]
  2. REST: The Definition [6]
  3. Web Architecture [6]
    1. Uniform Resource Identifier (URI) [2]
    2. Hypertext Transfer Protocol (HTTP) [3]
  4. Representations [7]
    1. Structured Documents [5]
    2. Linked Documents [2]
  5. State [6]
Hypertext Transfer Protocol (HTTP) D. Mahendran: Representational State Transfer (REST)

(20) RESTful Applications Talk HTTP

  • HTTP [URIs & HTTP; Hypertext Transfer Protocol (HTTP) (1)] is essential for implementing a Uniform Interface [Uniform Interface (1)]
    • HTTP defines a small set of methods for acting on URI-identified resources
  • Misusing HTTP turns application into non-RESTful applications
    • they lose the capability to be used just by adhering to REST principles
    • it's a bad sign when you think you need an interface description language
  • Extending HTTP turns applications into more specialized RESTful applications
    • may be appropriate when more operations are required
    • seriously reduces the number of potential clients


Hypertext Transfer Protocol (HTTP) D. Mahendran: Representational State Transfer (REST)

(21) HTTP Methods

  • Safe methods can be ignored or repeated without side-effects
    • arithmetically safe: 41 × 1 × 1 × 1 × 1 …
    • in practice, without side-effects means without relevant side-effects
  • Idempotent methods can be repeated without side-effects
    • arithmetically idempotent: 41 × 0 × 0 × 0 × 0 …
    • in practice, without side-effects means without relevant side-effects
  • Unsafe and non-idempotent methods should be treated with care
  • HTTP has two main safe methods: GET HEAD
  • HTTP has two main idempotent methods: PUT DELETE
  • HTTP has one main overload method: POST


Hypertext Transfer Protocol (HTTP) D. Mahendran: Representational State Transfer (REST)

(22) Cookies

  • Cookies [Web Storage; Cookies (1)] are client site state bound to a domain
    • they are convenient because they work without having to use a representation
    • they are inconvenient because they are not embedded in representations
  • Cookies are managed by the client
    • they are shared across browser tabs
    • they are not shared across browsers used by the same user
    • essentially, the client model of cookies is a bit outdated
  • Two major things to look out for when using cookies:
    1. session IDs are application state (i.e., non-resource state)
    2. cookies break the back button (requests contain a URI/cookie combo)
  • The ideal RESTful cookie is never sent to the server
    • cookies as persistent data storage on the client
    • interactions with the server are only using URIs and representations


Representations

Outline (Representations)

  1. Abstraction Layers [3]
  2. REST: The Definition [6]
  3. Web Architecture [6]
    1. Uniform Resource Identifier (URI) [2]
    2. Hypertext Transfer Protocol (HTTP) [3]
  4. Representations [7]
    1. Structured Documents [5]
    2. Linked Documents [2]
  5. State [6]

Structured Documents

Structured Documents D. Mahendran: Representational State Transfer (REST)

(25) What is identified by a URI?

  • Essential for implementing Self-Describing Messages [Self-Describing Messages (1)]
  • Resource Identification [Resource Identification (1)] only talks about an abstract resource
    • resources are never exchanged or otherwise processed directly
    • all interactions use resource representations
  • Representations depend on various factors
    • the nature of the resource
    • the capabilities of the server
    • the capabilities or the communications medium
    • the capabilities of the client
    • requirements and constraints from the application scenario
    • negotiations to figure out the best representation
  • Representations are identified by media type (sometimes called MIME type) [http://dret.net/lectures/web-fall08/mediatypes]


Structured Documents D. Mahendran: Representational State Transfer (REST)

(26) Extensible Markup Language (XML)

  • The language that started it all
    • created as a streamlined version of SGML
    • took over as the first universal language for structured data
  • XML is a metalanguage (a language for representing languages)
    • many domain-specific languages are defined as XML vocabularies
    • some metalanguages use XML syntax ([@rdf] is a popular example)
  • XML is only syntax and has almost zero semantics
    • very minimal built-in semantics (language identification, IDs, relative URIs)
    • semantics are entirely left to the XML vocabularies
  • XML is built around a tree model
    • each XML document is a tree and thus limited in structure
    • RESTful XML introduces hypermedia to turn XML data into a graph


Structured Documents D. Mahendran: Representational State Transfer (REST)

(27) JavaScript Object Notation (JSON)

  • The XMLHttpRequest [Scripting; JavaScript and XML (1)] API has been built for requesting XML via HTTP
    • this is useful because XML is the most popular data format
    • all requested data has to be processed by using XML access methods in JavaScript
  • JavaScript does not have XML as its internal data model
    • the XML received via XMLHttpRequest has to be parsed into a DOM tree
    • DOM access in JavaScript is inconvenient for complex operations
    • alternatively, the XML can be mapped to JavaScript objects (also requires parsing)
  • JavaScript Object Notation (JSON) encodes data as JavaScript objects
    • more efficient for the consumer if the consumer is written in JavaScript
    • this turns the generally usable XML service into a JavaScript-oriented service
    • for large-scale applications, it might make sense to provide XML and JSON
    • this can be negotiated with HTTP content negotiation


Structured Documents D. Mahendran: Representational State Transfer (REST)

(28) Resource Description Framework (RDF)

  • Developed around the same time as XML was developed
    • based on the idea of machine-readable/understandable semantics
    • builds the Semantic Web as a parallel universe on top of the Web
  • RDF uses URIs for naming things
    • RDF's data model is based on (URI, property, value) triples
    • triples are combined and inference is used to produce a graph
  • RDF is a metalanguage built on the triple-based data model
    • RDF has a number of syntaxes (one of them is XML [Extensible Markup Language (XML) (1)]-based)
    • RDF introduces a number of schema languages (often referred to as ontology languages)


Structured Documents D. Mahendran: Representational State Transfer (REST)

(29) Atom

  • A language for supporting Content Syndication [Content Syndication]
  • Much more modest in its goal than XML [Extensible Markup Language (XML) (1)] or [@rdf]
    • models feeds as a sets of entries with associated metadata
    • uses an XML vocabulary for representing the data model
    • uses links for expressing relationships in the data model
  • [@atompub] extends Atom with a RESTful protocol
    • many application scenarios might needs extensions to basic Atom/AtomPub


State

Outline (State)

  1. Abstraction Layers [3]
  2. REST: The Definition [6]
  3. Web Architecture [6]
    1. Uniform Resource Identifier (URI) [2]
    2. Hypertext Transfer Protocol (HTTP) [3]
  4. Representations [7]
    1. Structured Documents [5]
    2. Linked Documents [2]
  5. State [6]
State D. Mahendran: Representational State Transfer (REST)

(34) State Management on the Web



State D. Mahendran: Representational State Transfer (REST)

(35) State in HTML or HTTP

State in HTML or HTTP

State D. Mahendran: Representational State Transfer (REST)

(36) State in the Server Application

State in the Server Application

State D. Mahendran: Representational State Transfer (REST)

(37) State as a Resource

State as a Resource

State D. Mahendran: Representational State Transfer (REST)

(38) Stateless Shopping



State D. Mahendran: Representational State Transfer (REST)

(39) Reusing Resources

Reusing Resources

D. Mahendran: Representational State Transfer (REST)

(40) Conclusions



2011-11-10 Web Architecture [./]
Fall 2011 — INFO 253 (CCN 42598)