10. RESTfully Delicious
●
Get a list of all bookmarks
●
filter by user and/or tag
●
limit by number
●
Add a bookmark
●
Edit a bookmark
●
Delete a bookmark
11. Discover Resources
Resources URLs Methods Representations
Bookmark /bookmarks/{md5} GET application/bookmark+xml
PUT application/bookmark+xml
DELETE
Bookmark list /bookmarks GET application/atom+xml
Resources URLs Methods Representations
POST application/bookmark+xml
User list /users GET application/atom+xml
Users bookmarks /users/{user} GET application/atom+xml
Tag list /tags GET application/atom+xml
Tagged bookmarks /tags/{tag} GET application/atom+xml
Homepage / GET application/delicious+xml
12. GETting the Homepage
GET /
200 OK
Contenttype: application/delicious+xml
<?xml version=”1.0”?>
<delicious users=”/users” bookmarks=”/bookmarks” tags=”/tags”>
<recent start=”1” end=”20” next=”/?start=21&end=40”>
<bookmark url=quot;http://www.example.org/something
interstingquot;
href=quot;/bookmarks/a211528fb5108cddaa4b0d3aeccdbdcfquot;/>
...
</recent>
</delicious>
14. GETting A Bookmark
GET /bookmarks/a211528fb5108cddaa4b0d3aeccdbdcf
200 OK
Contenttype: application/bookmark+xml
<?xml version=quot;1.0quot;?>
<bookmark>
<title>Something interesting</title>
<url>http://example.org/somethingintersting</url>
<user href=”/users/pauljames”>pauljames</user>
<tags>
<tag href=”/tags/interesting”>interesting</tag>
</tags>
</bookmark>
15. Creating and Updating
POST /bookmarks
Contenttype: application/bookmark+xml
...
201 Created
Location: /bookmarks/a211528fb5108cddaa4b0d3aeccdbdcf
PUT /bookmarks/a211528fb5108cddaa4b0d3aeccdbdcf
Contenttype: application/bookmark+xml
...
200 OK
16. Benefits of Being RESTful
●
Better scaling due to stateless communications
●
Better response times due to caching
●
Better longterm compatibility due to:
●
the capability of document types to evolve without
breaking backwardscompatibility
●
the ability to add support for new content types
without reducing support for older content types.
●
Do less with more
17. Finally, some PHP
HTTP method
$_SERVER['REQUEST_METHOD']
Reading request data
$_SERVER['CONTENT_LENGTH']
$_SERVER['CONTENT_TYPE']
POST - $HTTP_RAW_POST_DATA
Other HTTP methods - fopen('php://input', 'r')
Processing request data
SimpleXML, MiniXML, PHP JSON, parse_str, unserialize,
unpack, preg_match, etc.
18. Generating Responses
HTTP response codes
200 OK, 201 Created, 204 No Content, 304 Not
Modified, 401 Unauthorized, 404 Not Found, 405
Method Not Allowed, 411 Length Required, 415
Unsupported Media Type
Generating representations
Smarty, PHPFastTemplate, GD, SimpleXML, printf, etc.
HTTP caching
Expires, Cache-Control, Etag, Last-Modified
header('Expires: '.gmdate('D, j M Y H:i:s T', time() +
$cachelength));
header('CacheControl: maxage='.$cachelength.', must
revalidate');
19. Conclusion
●
REST is not just about “Web Services”
●
Set of best practices for building Web apps
●
Give everything a URL
●
Use correct HTTP methods
●
Use common media types
●
Link things together with hypertext
20. Further Reading
●
Roger L. Costello (http://www.xfront.com/REST
WebServices.html)
●
Paul Prescod (http://www.prescod.net/rest/)
●
REST Wiki (http://rest.blueoxen.net/)
●
Atom Publishing Protocol
(http://atomenabled.org/developers/protocol/)
●
http://del.icio.us/tag/rest
21.
1
Introduction
Questions
Overloaded term
POX over HTTP?
Other HTTP methods?
Tidy URLs?
Buzzword?
http://flickr.com/photos/estherase/128983854/
22.
Representational State
Transfer
2
Architectural style
Distilled from the Web by Roy Fielding
HTTP 1.1 was designed to conform to REST
Defines how the Web works
Describes a set of rules for building applications on the
Web that exhibit certain desirable properties
REST is not HTTP, but HTTP is RESTful
“it’s the way the Web already works, just formalized a bit
and with some do’s and don’ts.”
Web service is a Web page that’s meant to be consumed
by an autonomous program as opposed to a Web
browser
http://flickr.com/photos/practicalowl/392894653/
23.
RESTful Principles
3
5 principles
●
A universal syntax for resourceidentification URLs
●
A set of welldefined operations
●
Having a shared set of mediatypes
●
The use of hypermedia for application statetransitions
●
Stateless protocol – all state on the client
http://flickr.com/photos/thowi/113223967/
24.
A universal syntax for
resource-identification
4
●
Uniform resource locators (URLs)
●
Every resource (thing of interest) has a URL
●
URLs are unique and allow us to dereference a
resource
●
Nouns
●
Trillions of nouns for all the concepts in all the heads
and files of all the people in the world
http://flickr.com/photos/joeholmes/258136938/
25.
A set of well-defined
operations
5
●
HTTP methods verbs
●
GET – fetch
●
POST append/process
●
PUT create/update
●
DELETE delete
●
Uniform interface, GET always gets, PUT always
creates
●
Using different verbs for different nouns would make
widespread communication impossible
●
There are no applications you can think of which
cannot be made to fit
http://flickr.com/photos/joygant/971783023/
26.
Having a shared set of
media-types
6
●
What's not machineprocessable about the current
Web isn't the protocol, it's the content
●
Information conveyed via documents
●
A standard set of document formats (HTML, RDF,
JPEG, PNG, etc.)
●
Representation of a resource
●
Resources are just concepts, representations are how
we interact with them
http://flickr.com/photos/thefrankfurtschool/1305454450/
27.
The use of hypermedia for
application state-transitions
7
●
Hypertext provides links between resources
●
Clients change state (navigate the Web) via information
from a previous state
●
URLs are opaque to clients, they never construct URLs
●
Because the links mirror the structure of how a user
makes progress through an application
●
A Webbased application is a dynamically changing
graph of state representations (pages) and potential
transitions (links) between states
●
If not, it may be accessible from the Web, but it’s not
really part of the Web
http://flickr.com/photos/pgoyette/100769956/
28.
Stateless
protocol
8
●
Application state is the information necessary to
understand the context of an interaction – auth details,
etc.
●
Resource state – S in REST, avoid unnamed state
●
All requests must include all application state
●
Session state is application state – if you want a
session you need a smarter client than a browser –
shopping cart
●
Prevents partial failures
●
Loadbalancing
●
Service interruptions
http://flickr.com/photos/davenyc/23033147/
29.
Building a
RESTful
App
9
●
Discover first class objects
●
Our resources
●
Assign URLs and URLspaces
●
Define representations
●
Input and output formats
●
Define HTTP methods
Time for an Example
http://flickr.com/photos/hugovk/2037935886/
30.
RESTfully Delicious
●
Get a list of all bookmarks
●
filter by user and/or tag
●
limit by number
●
Add a bookmark
●
Edit a bookmark
●
Delete a bookmark
10
Get a list of all bookmarks
filter by user and/or tag
limit by number
Add a bookmark
Edit a bookmark
Delete a bookmark
http://flickr.com/photos/sharynmorrow/124428600/
31.
Discover Resources
Resources URLs Methods Representations
Bookmark /bookmarks/{md5} GET application/bookmark+xml
PUT application/bookmark+xml
DELETE
Bookmark list /bookmarks GET application/atom+xml
Resources URLs Methods Representations
POST application/bookmark+xml
User list /users GET application/atom+xml
Users bookmarks /users/{user} GET application/atom+xml
Tag list /tags GET application/atom+xml
Tagged bookmarks /tags/{tag} GET application/atom+xml
Homepage / GET application/delicious+xml
11
http://flickr.com/photos/sharynmorrow/124428600/
32.
GETting the Homepage
GET /
200 OK
Contenttype: application/delicious+xml
<?xml version=”1.0”?>
<delicious users=”/users” bookmarks=”/bookmarks” tags=”/tags”>
<recent start=”1” end=”20” next=”/?start=21&end=40”>
<bookmark url=quot;http://www.example.org/something
interstingquot;
href=quot;/bookmarks/a211528fb5108cddaa4b0d3aeccdbdcfquot;/>
...
</recent>
</delicious>
12
http://flickr.com/photos/sharynmorrow/124428600/
34.
GETting A Bookmark
GET /bookmarks/a211528fb5108cddaa4b0d3aeccdbdcf
200 OK
Contenttype: application/bookmark+xml
<?xml version=quot;1.0quot;?>
<bookmark>
<title>Something interesting</title>
<url>http://example.org/somethingintersting</url>
<user href=”/users/pauljames”>pauljames</user>
<tags>
<tag href=”/tags/interesting”>interesting</tag>
</tags>
</bookmark>
14
http://flickr.com/photos/sharynmorrow/124428600/
35.
Creating and Updating
POST /bookmarks
Contenttype: application/bookmark+xml
...
201 Created
Location: /bookmarks/a211528fb5108cddaa4b0d3aeccdbdcf
PUT /bookmarks/a211528fb5108cddaa4b0d3aeccdbdcf
Contenttype: application/bookmark+xml
...
200 OK
15
http://flickr.com/photos/sharynmorrow/124428600/
36.
Benefits of Being RESTful
●
Better scaling due to stateless communications
●
Better response times due to caching
●
Better longterm compatibility due to:
●
the capability of document types to evolve without
breaking backwardscompatibility
●
the ability to add support for new content types
without reducing support for older content types.
●
Do less with more
16
Better scaling due to stateless communications
Better response times due to caching
Better longterm compatibility due to:
the capability of document types to evolve
without breaking backwardscompatibility
the ability to add support for new content types
without reducing support for older content
types.
Lower learning curve for consumer
Lower support overhead for producer
http://flickr.com/photos/ari/1387533615/
37.
Finally, some PHP
HTTP method
$_SERVER['REQUEST_METHOD']
Reading request data
$_SERVER['CONTENT_LENGTH']
$_SERVER['CONTENT_TYPE']
POST - $HTTP_RAW_POST_DATA
Other HTTP methods - fopen('php://input', 'r')
Processing request data
SimpleXML, MiniXML, PHP JSON, parse_str, unserialize,
unpack, preg_match, etc.
17
http://flickr.com/photos/nez/378348754/
38.
Generating Responses
HTTP response codes
200 OK, 201 Created, 204 No Content, 304 Not
Modified, 401 Unauthorized, 404 Not Found, 405
Method Not Allowed, 411 Length Required, 415
Unsupported Media Type
Generating representations
Smarty, PHPFastTemplate, GD, SimpleXML, printf, etc.
HTTP caching
Expires, Cache-Control, Etag, Last-Modified
header('Expires: '.gmdate('D, j M Y H:i:s T', time() +
$cachelength));
header('CacheControl: maxage='.$cachelength.', must
revalidate');
18
http://flickr.com/photos/nez/378348754/
39.
Conclusion
●
REST is not just about “Web Services”
●
Set of best practices for building Web apps
●
Give everything a URL
●
Use correct HTTP methods
●
Use common media types
●
Link things together with hypertext
19
REST is not just about “Web Services”
Set of best practices for building Web apps
Give everything a URL
Use correct HTTP methods
Use common media types
Link things together with hypertext
REST is an architectural style
It defines 4 core principles
A universal syntax for resource-
identification
A set of well-defined operations
Having a shared set of media-types
The use of hypermedia for application
state-transitions
Stateless client/server interaction
It helps us write well behaved apps
40.
Further Reading
●
Roger L. Costello (http://www.xfront.com/REST
WebServices.html)
●
Paul Prescod (http://www.prescod.net/rest/)
●
REST Wiki (http://rest.blueoxen.net/)
●
Atom Publishing Protocol
(http://atomenabled.org/developers/protocol/)
●
http://del.icio.us/tag/rest
20
http://flickr.com/photos/dhammza/91435718/