designing http services and restful interfaces (phpnw10 2010-10-09)
DESCRIPTION
Presentation given at PHPNW10.TRANSCRIPT
![Page 1: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/1.jpg)
DESIGNING HTTP SERVICESAND RESTFUL INTERFACES
![Page 2: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/2.jpg)
David Zülke
![Page 3: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/3.jpg)
David Zuelke
![Page 4: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/4.jpg)
![Page 5: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/5.jpg)
![Page 6: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/6.jpg)
(no need to be afraid)
![Page 7: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/7.jpg)
(we’re all friends now)
![Page 8: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/8.jpg)
http://en.wikipedia.org/wiki/File:München_Panorama.JPG
![Page 9: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/9.jpg)
Founder
![Page 11: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/11.jpg)
Lead Developer
![Page 14: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/14.jpg)
THE OLDEN DAYSBefore REST Was En Vogue
![Page 15: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/15.jpg)
http://www.acme.com/index.php?action=zomg&page=lol
![Page 16: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/16.jpg)
along came
![Page 17: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/17.jpg)
dis is srs SEO bsns
![Page 18: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/18.jpg)
and said
![Page 19: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/19.jpg)
NEIN NEIN NEIN NEIN
DAS IST VERBOTEN
![Page 20: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/20.jpg)
at least if they were
![Page 21: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/21.jpg)
![Page 22: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/22.jpg)
so we had to change this
![Page 23: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/23.jpg)
http://www.acme.com/zomg/lol
![Page 24: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/24.jpg)
and then things got out of control
![Page 25: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/25.jpg)
because nobody really had a clue
![Page 26: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/26.jpg)
http://acme.com/videos/latest/hamburgers
![Page 27: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/27.jpg)
http://acme.com/search/lolcats/pictures/yes/1/200
![Page 28: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/28.jpg)
oh dear…
![Page 29: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/29.jpg)
ALONG CAME ROY FIELDINGAnd Gave Us REST
![Page 30: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/30.jpg)
that was awesome
![Page 31: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/31.jpg)
because everyone could say
![Page 32: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/32.jpg)
I haz REST nao
![Page 33: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/33.jpg)
when in fact
![Page 34: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/34.jpg)
they bloody didn’t
![Page 35: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/35.jpg)
RESTWhat Does That Even Mean?
![Page 36: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/36.jpg)
REpresentational State Transfer
![Page 37: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/37.jpg)
•A URL identifies a Resource
•Resources have a hierarchy
• so you know that something with additional slashes is a subordinate resource
•Methods perform operations on resources
•The operation is implicit and not part of the URL
•A hypermedia format is used to represent the data
•Link relations are used to navigate a service
![Page 38: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/38.jpg)
and most importantly
![Page 39: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/39.jpg)
a web page is not a resource
![Page 40: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/40.jpg)
it is a representation of a resource
![Page 41: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/41.jpg)
GET /products/ HTTP/1.1Host: acme.comAccept: application/json
HTTP/1.1 200 OKContent-‐Type: application/json; charset=utf-‐8Allow: GET, POST
[ { id: 1234, name: "Red Stapler", price: 3.14, location: "http://acme.com/products/1234" }]
GETTING JSON BACK
![Page 42: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/42.jpg)
GET /products/ HTTP/1.1Host: acme.comAccept: application/xml
HTTP/1.1 200 OKContent-‐Type: application/xml; charset=utf-‐8Allow: GET, POST
<?xml version="1.0" encoding="utf-‐8"?><products xmlns="urn:com.acme.products" xmlns:xl="http://www.w3.org/1999/xlink"> <product id="1234" xl:type="simple" xl:href="http://acme.com/products/1234"> <name>Red Stapler</name> <price currency="EUR">3.14</price> </product></products>
GETTING XML BACK
![Page 43: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/43.jpg)
no hypermedia formats yet in those examples!
![Page 44: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/44.jpg)
I will show that in a few minutes
![Page 45: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/45.jpg)
GET /products/ HTTP/1.1Host: acme.comAccept: application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,*/*;q=0.5User-‐Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_8; en-‐us) AppleWebKit…
HTTP/1.1 200 OKContent-‐Type: text/html; charset=utf-‐8Allow: GET, POST
<html lang="en"> <head> <meta http-‐equiv="Content-‐Type" content="text/html; charset=UTF-‐8"></meta> <title>ACME Inc. Products</title> </head> <body> <h1>Our Incredible Products</h1> <ul id="products"> <li><a href="http://acme.com/products/1234">Red Stapler</a> (€3.14)</li> </ul> </body></html>
AND FINALLY, HTML
![Page 46: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/46.jpg)
A FEW EXAMPLESLet’s Start With Proper URL Design
![Page 47: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/47.jpg)
BAD URLS
• http://www.acme.com/product/
• http://www.acme.com/product/filter/cats/desc
• http://www.acme.com/product/1234
• http://www.acme.com/photos/product/1234
• http://www.acme.com/photos/product/1234/new
• http://www.acme.com/photos/product/1234/5678
WTF?
sausage ID?
new what?
![Page 48: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/48.jpg)
GOOD URLS
• http://www.acme.com/products/
• http://www.acme.com/products/?filter=cats&sort=desc
• http://www.acme.com/products/1234
• http://www.acme.com/products/1234/photos/
• http://www.acme.com/products/1234/photos/?sort=latest
• http://www.acme.com/products/1234/photos/5678
a list of productsfiltering is a query
a single productall photos
![Page 49: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/49.jpg)
THE NEXT LEVELTime To Throw CRUD Into The Mix
![Page 50: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/50.jpg)
COLLECTION OPERATIONS
• http://www.acme.com/products/
• GET to retrieve a list of products
• POST to create a new product
• returns
• 201 Created
• Location: http://www.acme.com/products/1235
![Page 51: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/51.jpg)
ITEM OPERATIONS
• http://www.acme.com/products/1234
• GET to retrieve
• PUT to update
•DELETE to, you guessed it, delete
![Page 52: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/52.jpg)
(bonus points if you spotted the CRUD there)
![Page 53: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/53.jpg)
HATEOASThe Missing Piece in the Puzzle
![Page 54: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/54.jpg)
ONE LAST PIECE IS MISSING
• How does a client know what to do with resources?
• How do you go to the “next” operation?
•What are the URLs for creating subordinate resources?
•Where is the contract for the service?
![Page 55: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/55.jpg)
HYPERMEDIA AS THE ENGINE OF APPLICATION STATE
• Use links to allow clients to discover locations and operations
• Link relations are used to express the possible options
• Clients do not need to know URLs, so they can change
• The entire application workflow is abstracted, thus changeable
• The hypermedia type itself can be versioned if necessary
•No breaking of clients if the implementation is updated!
![Page 56: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/56.jpg)
XHTML and Atom are Hypermedia formats
![Page 57: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/57.jpg)
Or you roll your own...
![Page 58: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/58.jpg)
GET /products/1234 HTTP/1.1Host: acme.comAccept: application/vnd.acmecorpshop+xml
HTTP/1.1 200 OKContent-‐Type: application/vnd.acmecorpshop+xml; charset=utf-‐8Allow: GET, PUT, DELETE
<?xml version="1.0" encoding="utf-‐8"?><product xmlns="urn:com.acme.prods" xmlns:atom="http://www.w3.org/2005/xlink"> <id>1234</id> <name>Red Stapler</name> <price currency="EUR">3.14</price> <atom:link rel="payment" type="application/vnd.acmecorpshop+xml" href="http://acme.com/products/1234/payment"/></product>
re-use Atom forlink relations
meaning defined in Atom standard!
A CUSTOM MEDIA TYPE
![Page 59: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/59.jpg)
XML is really good for hypermedia formats
![Page 60: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/60.jpg)
(hyperlinks, namespaced attributes, re-use of formats, …)
![Page 61: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/61.jpg)
JSON is more difficult
![Page 62: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/62.jpg)
(no hyperlinks, no namespaces, no element attributes)
![Page 63: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/63.jpg)
<?xml version="1.0" encoding="utf-‐8"?><product xmlns="urn:com.acme.prods" xmlns:atom="http://www.w3.org/2005/xlink"> <id>1234</id> <name>Red Stapler</name> <price currency="EUR">3.14</price> <atom:link rel="payment" type="application/vnd.acmecorpshop+xml" href="http://acme.com/products/1234/payment"/></product>
{ id: 1234, name: "Red Stapler", price: { amount: 3.14, currency: "EUR" }, links: [ { rel: "payment", type: "application/vnd.acmecorpshop+xml", href: "http://acme.com/products/1234/payment" } ]}
XML VERSUS JSON
![Page 64: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/64.jpg)
and hey
![Page 65: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/65.jpg)
without hypermedia, your HTTP interface is not RESTful
![Page 66: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/66.jpg)
that’s totally fineand sometimes even the only way to do it
![Page 67: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/67.jpg)
(e.g. CouchDB or S3 are never going to be RESTful)
![Page 68: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/68.jpg)
but don’t you dare call it a RESTful interface
![Page 69: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/69.jpg)
YOU MIGHT BE WONDERINGWhy Exactly Is This Awesome?
![Page 70: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/70.jpg)
because it scales
![Page 71: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/71.jpg)
not just terms of performance
![Page 72: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/72.jpg)
but also in how you can extend and evolve it
![Page 73: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/73.jpg)
and how it interoperates with the Web of today
![Page 74: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/74.jpg)
it’s completely seamless
![Page 75: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/75.jpg)
all thanks to the polymorphism of URLs
![Page 76: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/76.jpg)
the “soft transitions” you can achieve with link relations
![Page 77: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/77.jpg)
and all the features HTTP has to offer*
*: if you’re using REST over HTTP
![Page 78: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/78.jpg)
HTTP GOODIES
• Content Negotiation
• Redirection
• Authentication
• Transport Layer Security
• Caching
• Load Balancing
![Page 79: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/79.jpg)
but remember this
![Page 80: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/80.jpg)
don’t use sessions, logins or cookies to maintain state
![Page 81: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/81.jpg)
TWITTERS “REST” API, DISSECTED
Let’s Look At The Status Methods
![Page 82: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/82.jpg)
• GET http://api.twitter.com/1/statuses/show/id.format
• Problems:
•Operation (“show”) included in the URL
• Status ID not a child of the “statuses” collection
• Better : GET http://twitter.com/statuses/id with Accept header
STATUSES/SHOW
![Page 83: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/83.jpg)
• POST http://api.twitter.com/1/statuses/update.format
• Problems:
•Operation (“update”) included in the URL
• Uses the authenticated user implicitly
• Better : POST http://twitter.com/users/id/statuses/
STATUSES/UPDATE
![Page 84: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/84.jpg)
• POST http://api.twitter.com/1/statuses/destroy/id.format
• Problems:
•Operation (“destroy”) included in the URL like it’s 1997
•Odd, illogical hierarchy again
• Allows both “POST” and “DELETE” as verbs
• Better : DELETE http://twitter.com/statuses/id
STATUSES/DESTROY
![Page 85: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/85.jpg)
• GET http://api.twitter.com/1/statuses/retweets/id.format
• Problems:
• Hierarchy is wrong
• Better : GET http://twitter.com/statuses/id/retweets/
STATUSES/RETWEETS
![Page 86: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/86.jpg)
• PUT http://api.twitter.com/1/statuses/retweet/id.format
• Problems:
• “retweets” collection exists, but is not used here
• As usual, the action is in the URL (“make retweet” is RPC-y)
• Allows both “PUT” and “POST” as verbs
• Better : POST http://twitter.com/statuses/id/retweets/
STATUSES/RETWEET
![Page 87: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/87.jpg)
SUMMARY
• http://twitter.com/statuses/
• POST to create a new tweet
• http://twitter.com/statuses/12345
•DELETE deletes, PUT could be used for updates
• http://twitter.com/statuses/12345/retweets
• POST creates a new retweet
![Page 88: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/88.jpg)
HOSTS AND VERSIONING
•Q: Why not http://api.twitter.com/ ?
• A: Because http://api.twitter.com/statuses/1234 and http://twitter.com/statuses/1234 would be different resources!
•Q: What about /1/ or /2/ for versioning?
• A: Again, different resources. Instead, use the media type:application/vnd.com.twitter.api.v1+xml orapplication/vnd.com.twitter.api+xml;ver=2
![Page 89: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/89.jpg)
FURTHER READING
• Ryan TomaykoHow I Explained REST to my Wifehttp://tomayko.com/writings/rest-to-my-wife
• Jim Webber, Savas Parastatidis & Ian RobinsonHow to GET a Cup of Coffeehttp://www.infoq.com/articles/webber-rest-workflow
• Roy Thomas FieldingArchitectural Styles and the Design of Network-based Software Architectureshttp://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm
![Page 90: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/90.jpg)
!e End
![Page 91: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/91.jpg)
Questions?
![Page 92: Designing HTTP Services and RESTful Interfaces (PHPNW10 2010-10-09)](https://reader038.vdocuments.us/reader038/viewer/2022103014/54b7a7994a7959b0218b45d1/html5/thumbnails/92.jpg)
THANK YOU!This was a presentation by
@dzuelkeSend me an e-mail!