REST

Representational State Transfer
whoami

Colin Harrington
 ● colin.harrington@objectpartners.com
 ● colin@colinharrington.net
 ● http://colinharrington.net
 ● @ColinHarrington
History

HTTP :: HyperText Transfer Protocol
Tim Berners-Lee :: 1990s

Foundations of the Web

OSI Layer 7
Application Layer


Protocol
History

REST :: Representational State Transfer

2000 Doctoral Dissertation by Roy T. Fielding
http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm

"Roy Thomas Fielding (born 1965) is an American computer
scientist[1], one of the principal authors of the HTTP specification, an
authority on computer network architecture[2] and co-founder of the
Apache HTTP Server project." (Wikipedia)
http://roy.gbiv.com/

@fielding
History - REST
Architecture:
 ● Client-Server
 ● Stateless + Cacheable
 ● Uniform Interface + Layerable

Data:
 ● Resources, identifiers & metadata
 ● Representation & metadata
 ● Control Data { headers }

Elements:
 ● {clients, server, resolver, cache}
 ● Gateways, proxies, user agents
 ● URL, URI, Schemes
REST

REST is a buzzword and a movement but it symbolizes a coherent
usage of the web (HTTP) as it was designed




            REST ~ HTTP done right.
Resources

Uniform Resource Identifiers (URI)

       <scheme>:<scheme-specified-structure>

 ● ftp://example.org/resource.txt
 ● urn:issn:1535-3613
 ● mailto:colin@colinharrington.net


Uniform Resource Locator (URL)

            scheme://domain:port/path?query_string
HTTP Methods/Verbs

HEAD, GET, OPTIONS => considered "safe"
PUT, POST, DELETE => possibly destructive
TRACE, DEBUG => Considered harmful


curl -v http://www.google.com

GET / HTTP/1.1
User-Agent: curl/7.21.0
Host: www.google.com
Accept: */*
Accept Headers :: Client

GET / HTTP/1.1
User-Agent: curl/7.21.0
Host: www.google.com
Accept: */*


Accept: application/xml


Accept: text/html,application/xhtml+xml,
application/xml;q=0.9,*/*;q=0.8,
application/json
HTTP Status Codes

200 - OK
301 - Moved permanently
302 - Moved temporarily
404 - Not Found
500
            http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

HTTP/1.1 200 OK
Date: Mon, 31 Oct 2011 16:07:53 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=ISO-8859-1
http://homestarrunner.com/404d
Representations

                    JSON Example
 ● XML              {
                         "firstName": "John",
                         "lastName" : "Smith",
                         "age"       : 25,
 ● JSON                  "address" :
                         {
                             "streetAddress": "21 2nd Street",
                             "city"          : "New York",
 ● HTML                      "state"         : "NY",
                             "postalCode"    : "10021"
                         },
                         "phoneNumber":
 ● XHTML                 [
                             {
                                "type" : "home",
                                "number": "212 555-1234"
 ● RDF                       },
                             {
                                "type" : "fax",
                                "number": "646 555-4567"
 ● Text                  ]
                             }

                      }

 ● Custom Formats
Content Negotiation

Request:
Accept Header
 Accept: application/xml

Accept-Language
Accept-Encoding

Response:
Content-Type: text/json

Accept: text/html,application/xhtml+xml,
application/xml;q=0.9,*/*;q=0.8,
application/json
Cacheability

HTTP Cache Headers

 ● Expires
 ● Cache-Control
 ● ETag
 ● Last-Modified
 ● Pragma no-cache

Proxy & Network path.
CRUD

                                            Create :: POST

                                              Read :: GET

                                             Update :: PUT

                                         Delete :: DELETE




* http://stackoverflow.com/questions/630453/put-vs-post-in-rest
Hypermedia

Linking
          to other resources / media

HTML documents, resources
loosely coupled
Images, CSS, favicon, etc.




Like this image ->
http://cuip.uchicago.edu/~cac/images/Hypermedia.jpg
Who

http://www.programmableweb.com/

Amazon Web Services
 {S3, EC2, SQS, RDS, FPS, etc.}




Just to name a few...
Resources
This guy explains REST in to his non-technical wife:
http://tomayko.com/writings/rest-to-my-wife

Good article on how to REST with curl:
http://blogs.plexibus.com/2009/01/15/rest-esting-with-curl/

Poster -- the Firefox plugin:
https://addons.mozilla.org/en-US/firefox/addon/poster/

You'd probably learn best by actually making some RESTful
calls. Pick a service that is free and RESTful and play around with it. I'd
recommend using http://www.twilio.com/ just because they offer you
$30 in free credit and you get to make, receive and control real phone
calls.
Thank You

Brief Introduction to REST

  • 1.
  • 2.
    whoami Colin Harrington ●[email protected][email protected] ● http://colinharrington.net ● @ColinHarrington
  • 3.
    History HTTP :: HyperTextTransfer Protocol Tim Berners-Lee :: 1990s Foundations of the Web OSI Layer 7 Application Layer Protocol
  • 4.
    History REST :: RepresentationalState Transfer 2000 Doctoral Dissertation by Roy T. Fielding http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm "Roy Thomas Fielding (born 1965) is an American computer scientist[1], one of the principal authors of the HTTP specification, an authority on computer network architecture[2] and co-founder of the Apache HTTP Server project." (Wikipedia) http://roy.gbiv.com/ @fielding
  • 5.
    History - REST Architecture: ● Client-Server ● Stateless + Cacheable ● Uniform Interface + Layerable Data: ● Resources, identifiers & metadata ● Representation & metadata ● Control Data { headers } Elements: ● {clients, server, resolver, cache} ● Gateways, proxies, user agents ● URL, URI, Schemes
  • 6.
    REST REST is abuzzword and a movement but it symbolizes a coherent usage of the web (HTTP) as it was designed REST ~ HTTP done right.
  • 7.
    Resources Uniform Resource Identifiers(URI) <scheme>:<scheme-specified-structure> ● ftp://example.org/resource.txt ● urn:issn:1535-3613 ● mailto:[email protected] Uniform Resource Locator (URL) scheme://domain:port/path?query_string
  • 8.
    HTTP Methods/Verbs HEAD, GET,OPTIONS => considered "safe" PUT, POST, DELETE => possibly destructive TRACE, DEBUG => Considered harmful curl -v http://www.google.com GET / HTTP/1.1 User-Agent: curl/7.21.0 Host: www.google.com Accept: */*
  • 9.
    Accept Headers ::Client GET / HTTP/1.1 User-Agent: curl/7.21.0 Host: www.google.com Accept: */* Accept: application/xml Accept: text/html,application/xhtml+xml, application/xml;q=0.9,*/*;q=0.8, application/json
  • 10.
    HTTP Status Codes 200- OK 301 - Moved permanently 302 - Moved temporarily 404 - Not Found 500 http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html HTTP/1.1 200 OK Date: Mon, 31 Oct 2011 16:07:53 GMT Expires: -1 Cache-Control: private, max-age=0 Content-Type: text/html; charset=ISO-8859-1
  • 11.
  • 12.
    Representations JSON Example ● XML { "firstName": "John", "lastName" : "Smith", "age" : 25, ● JSON "address" : { "streetAddress": "21 2nd Street", "city" : "New York", ● HTML "state" : "NY", "postalCode" : "10021" }, "phoneNumber": ● XHTML [ { "type" : "home", "number": "212 555-1234" ● RDF }, { "type" : "fax", "number": "646 555-4567" ● Text ] } } ● Custom Formats
  • 13.
    Content Negotiation Request: Accept Header Accept: application/xml Accept-Language Accept-Encoding Response: Content-Type: text/json Accept: text/html,application/xhtml+xml, application/xml;q=0.9,*/*;q=0.8, application/json
  • 14.
    Cacheability HTTP Cache Headers ● Expires ● Cache-Control ● ETag ● Last-Modified ● Pragma no-cache Proxy & Network path.
  • 15.
    CRUD Create :: POST Read :: GET Update :: PUT Delete :: DELETE * http://stackoverflow.com/questions/630453/put-vs-post-in-rest
  • 16.
    Hypermedia Linking to other resources / media HTML documents, resources loosely coupled Images, CSS, favicon, etc. Like this image -> http://cuip.uchicago.edu/~cac/images/Hypermedia.jpg
  • 17.
  • 18.
    Resources This guy explainsREST in to his non-technical wife: http://tomayko.com/writings/rest-to-my-wife Good article on how to REST with curl: http://blogs.plexibus.com/2009/01/15/rest-esting-with-curl/ Poster -- the Firefox plugin: https://addons.mozilla.org/en-US/firefox/addon/poster/ You'd probably learn best by actually making some RESTful calls. Pick a service that is free and RESTful and play around with it. I'd recommend using http://www.twilio.com/ just because they offer you $30 in free credit and you get to make, receive and control real phone calls.
  • 19.