REST
representational state transfer
David Krmpotić
WHAT IS IT?
Architectural style that builds on
the existing protocols of the web.
Roy Fielding
http://app.com/pics/show/1
http://app.com/pics/destroy/1
RESTful style knocks
the verbs out of the urls
http://app.com/pics/1
What remains:
a (unique) resource identifier
an “abstract thing” that lives in the cloud
a.k.a. ”the intended conceptual target of a
hypertext reference” :]
a representation of the resource
(you can never access the resource itself, only various representations of it)
http:// GET app.com/pics/1
We have nouns (resources),
but what happened to the verbs?
http:// DELETE app.com/pics/1
 they moved from the URL to the HTTP request method !
IS THIS A
GOOD THING?
simplicity and abstraction
GET
PUT
POST
DELETE
Yes!
} uniform
interface
REST is about resources
Nouns
URIs
(unconstrained)
Verbs
GET, PUT, POST, DELETE
(constrained)
Content Types
HTML, JPEG, XML etc.
(constrained)
GET /pics/1
Accept: JPEG
or GET /pics/1.jpg
Requesting different
resource representations
GET /pics/1
Accept: XML
or GET /pics/1.xml
<picture>
<place>Axe Lake</place>
<size>1024x768</size>
<date>2008-05-01</date>
</picture>
GET /people
Accept: XML
Plural resources
GET /people/1
Accept: JPEG
<people>
<person>
<id>1</id>
<name>Ann</name>
<age>28</age>
</person>
<person>
<id>2</id>
<name>Bruce</name>
<age>32</age>
</person>
...
</people>
It relies on the following standards though:
HTTP
URL
XML/HTML/GIF/JPEG/etc
(resource representations)
text/xml, text/html, image/gif, image/jpeg, etc.
(content types)
remember
REST is not a standard!
REST in
Before enlightenment
http://app.com/clients/show/1
After
controller action
http://app.com/clients/1
+ HTTP method
controllermap.resources
action
map.resources
action where the request lands is shown with gray background
edit and new are helper actions for showing the form
We also get a few named routes:
clients_path
new_client_path
edit_client_path(id)
client_path(id)
Custom actions
GET /tasks/completed PUT /tasks/12/complete
you should add custom actions carefully and never more than two or three
Responder block
takes care of different resource representations
Nested resources (in Rails 2)
http://adam.blog.heroku.com/past/2007/12/20/nested_resources_in_rails_2/
/events/1/tickets
A resource is not always a one-to-one
mapping to a Rails model or database table.
Sometimes a resource is "virtual" and exists in
your domain vocabulary and application logic,
but is not backed by a database table. [2]
GOOD TO KNOW
models != resources
To create a website that is:
• easy to use
• easy to maintain
• easy to cache
• easy to scale
Why use REST?
Thank you!
Resources (n.p.i.)
[1] Roy Fielding’s dissertation (2000)
www.ics.uci.edu/~fielding/pubs/dissertation/top.htm
[2] REST 101
www.softiesonrails.com/search?q=rest+101
[3] A Brief Introduction to REST
www.infoq.com/articles/rest-introduction
[4] How I Explained REST to My Wife
tomayko.com/writings/rest-to-my-wife

Representational State Transfer (REST)