Your browser doesn't support the features required by impress.js, so you are presented with a simplified version of this presentation.

For the best experience please use the latest Chrome, Safari or Firefox browser.


REST

The Architectural Style of the Web

Prof. Cesare Pautasso
http://www.pautasso.info
cesare.pautasso@usi.ch
@pautasso

REpresentational State Transfer

REST defines the architectural style of the Web

Four design principles explain the success and the scalability of the HTTP protocol

  1. Resource Identification through URI
  2. Uniform Interface for all resources:
    • GET (query the state, idempotent, can be cached)
    • POST (Create/Access a resource)
    • PUT (Create/Update a resource)
    • DELETE (Delete a resource)
  3. Multiple representations of the same resource
  4. Hyperlinks model resource relationships and valid state transitions for dynamic protocol description and discovery

URI tell a story

How do we find something on the Internet?

URI example

ftp://user:pass@ftp.usi.ch/root/public/videos/introduction.avi

Start your remote file transfer client, connect to ftp.usi.ch using the ftp protocol on port 21, login using your local account and password, go to my public folder root/public and then change to the videos sub-folder and download a copy of the introduction.avi file.

Uniform Resource Identifier

Internet Standard for resource naming and identification (originally from 1994, revised until 2005)

Nice URIs

https://maps.google.com/maps?q=lugano&hl=en&ll=46.004906,8.954093& spn=0.01632,0.030148&sll=37.0625,-95.677068&sspn=73.825956,123.486328& t=m&hnear=Lugano,+Ticino,+Switzerland&z=16

URI Design Tips

Parametric URIs

http://map.com/search/Lugano/Parking

Prefer positional encoding

http://map.com/search?where=Lugano&what=Parking

Key-value encoding (useful for optional parameters)

Uniform Interface

HTTP MethodSafeIdempotent
POSTCreate a sub resource (Perform an action)??
GETRetrieve the current state of the resourceYESYES
PUTCreate or update the state of a resourceNOYES
DELETEClear a resource (invalidate its URI)NOYES

POST vs. GET

POST vs. PUT

Resources are created by many concurrent clients

What is the right way of creating resources and to initialize their state?

PUT /resource/{id}
201 Created
  • Problem: How to ensure resource {id} is unique?
  • Solution 1: let the client choose a unique id (GUID)
POST /resource
301 Moved Permanently
Location: /resource/{id}
  • Solution 2: let the server compute the unique id
  • Problem: Duplicate resource instances may be created if requests are repeated due to unreliable communication

Representations

Resources may have multiple representations

Resource representations are controlled with meta-data (HTTP Headers)

Hypermedia

Problem: How to discover the URIs of a potentially infinite and dynamically changing set of resources?

Solution: Resource Representations contain links to other resources

Discovery by Referral

  • Clients can use a service to dynamically lookup and discover other services
  • Any resource can refer clients to any other resource (decentralized)
  • Links can be embedded and found in any hypermedia representation format

Design Process

  1. Identify resources to be exposed as services (e.g., photoalbum images, book catalog, purchase order, open bugs, blog entries, polls and votes)
  2. Model relationships (e.g., containment, reference, state transitions) between resources with hyperlinks that can be followed to get more details (or perform state transitions)
  3. Define URIs to address the resources
  4. Understand what it means to do a GET, POST, PUT, DELETE for each resource (and whether it is allowed or not)
  5. Design, document and standardize resource representations (media types)
GETPUTPOSTDELETE
/loan
/balance
/user
/book
/order?

Simple Doodle API Example

  1. Resources: Polls and Votes
  2. Relationships: Containment
  3. URIs embed IDs of "child" instance resources
  4. POST on the container creates new child resources
  5. PUT/DELETE for updating and removing child resources
GETPUTPOSTDELETE
/poll
/poll/{id}
/poll/{id}/vote
/poll/{id}/vote/{id}

Creating a poll

POST /poll
<options>A,B,C</options>
201 Created
Location: /poll/42

Reading a poll

GET /poll/42
200 OK
<options>A,B,C</options>
<votes href="/vote"/>

Cast a vote

POST /poll/42/vote
<name>C. Pautasso</name>
<choice>B</choice>
201 Created
Location: /poll/42/vote/1
GET /poll/42
200 OK
<options>A,B,C</options>
<votes href="/vote">
 <vote id="1">
  <name>C. Pautasso</name>
  <choice>B</choice>
 </vote>
</votes>

Update a vote

PUT /poll/42/vote/1
<name>C. Pautasso</name>
<choice>C</choice>
200 OK
GET /poll/42
200 OK
<options>A,B,C</options>
<votes href="/vote">
 <vote id="1">
  <name>C. Pautasso</name>
  <choice>C</choice>
 </vote>
</votes>

Remove a poll

DELETE /poll/42
200 OK

Poll is deleted

GET /poll/42
404 Not Found

References

Use a spacebar or arrow keys to navigate