HTTP & REST - Tufts University · GET /demo1.html HTTP/1.0 . Host: webarch.noahdemo.com ....
Transcript of HTTP & REST - Tufts University · GET /demo1.html HTTP/1.0 . Host: webarch.noahdemo.com ....
![Page 1: HTTP & REST - Tufts University · GET /demo1.html HTTP/1.0 . Host: webarch.noahdemo.com . User-Agent: ... In practice: – Cookies stored in browser tied to session state and login](https://reader031.fdocuments.in/reader031/viewer/2022021821/5b09f3247f8b9a45518b7808/html5/thumbnails/1.jpg)
HTTP & REST
Noah Mendelsohn Tufts University Email: [email protected] Web: http://www.cs.tufts.edu/~noah
COMP 150-IDS: Internet Scale Distributed Systems (Spring 2018)
Copyright 2012, 2103, 2015, 2017 & 2018
![Page 2: HTTP & REST - Tufts University · GET /demo1.html HTTP/1.0 . Host: webarch.noahdemo.com . User-Agent: ... In practice: – Cookies stored in browser tied to session state and login](https://reader031.fdocuments.in/reader031/viewer/2022021821/5b09f3247f8b9a45518b7808/html5/thumbnails/2.jpg)
© 2010 Noah Mendelsohn
Architecting a universal Web
Identification: URIs
Interaction: HTTP
Data formats: HTML, JPEG, GIF, etc.
![Page 3: HTTP & REST - Tufts University · GET /demo1.html HTTP/1.0 . Host: webarch.noahdemo.com . User-Agent: ... In practice: – Cookies stored in browser tied to session state and login](https://reader031.fdocuments.in/reader031/viewer/2022021821/5b09f3247f8b9a45518b7808/html5/thumbnails/3.jpg)
© 2010 Noah Mendelsohn 3
Goals
Learn why traditional architectures would not have worked for the Web
Learn the basics of HTTP (review)
Explore some interesting characteristics of HTTP
Learn the REST distributed computing architecture
![Page 4: HTTP & REST - Tufts University · GET /demo1.html HTTP/1.0 . Host: webarch.noahdemo.com . User-Agent: ... In practice: – Cookies stored in browser tied to session state and login](https://reader031.fdocuments.in/reader031/viewer/2022021821/5b09f3247f8b9a45518b7808/html5/thumbnails/4.jpg)
© 2010 Noah Mendelsohn
Question: What would happen if we built the
Web from RPC?
![Page 5: HTTP & REST - Tufts University · GET /demo1.html HTTP/1.0 . Host: webarch.noahdemo.com . User-Agent: ... In practice: – Cookies stored in browser tied to session state and login](https://reader031.fdocuments.in/reader031/viewer/2022021821/5b09f3247f8b9a45518b7808/html5/thumbnails/5.jpg)
© 2010 Noah Mendelsohn
Why not use RPC for the Web?
No uniform interface – You’d need an interface definition to follow a link
No global naming
Most RPC data types poorly tuned for documents – Int, float, struct not HTML, XML, mixed-content, etc.
We’ll revisit the RPC/Web question later and add some more
![Page 6: HTTP & REST - Tufts University · GET /demo1.html HTTP/1.0 . Host: webarch.noahdemo.com . User-Agent: ... In practice: – Cookies stored in browser tied to session state and login](https://reader031.fdocuments.in/reader031/viewer/2022021821/5b09f3247f8b9a45518b7808/html5/thumbnails/6.jpg)
© 2010 Noah Mendelsohn
HTTP in Action (review from first week)
![Page 7: HTTP & REST - Tufts University · GET /demo1.html HTTP/1.0 . Host: webarch.noahdemo.com . User-Agent: ... In practice: – Cookies stored in browser tied to session state and login](https://reader031.fdocuments.in/reader031/viewer/2022021821/5b09f3247f8b9a45518b7808/html5/thumbnails/7.jpg)
© 2010 Noah Mendelsohn
The user clicks on a link
URI is http://webarch.noahdemo.com/demo1/test.html URI is http://webarch.noahdemo.com/demo1/test.html
![Page 8: HTTP & REST - Tufts University · GET /demo1.html HTTP/1.0 . Host: webarch.noahdemo.com . User-Agent: ... In practice: – Cookies stored in browser tied to session state and login](https://reader031.fdocuments.in/reader031/viewer/2022021821/5b09f3247f8b9a45518b7808/html5/thumbnails/8.jpg)
© 2010 Noah Mendelsohn
The http “scheme” tells client to send HTTP GET msg
HTTP GET
URI is http://webarch.noahdemo.com/demo1/test.html URI is http://webarch.noahdemo.com/demo1/test.html
![Page 9: HTTP & REST - Tufts University · GET /demo1.html HTTP/1.0 . Host: webarch.noahdemo.com . User-Agent: ... In practice: – Cookies stored in browser tied to session state and login](https://reader031.fdocuments.in/reader031/viewer/2022021821/5b09f3247f8b9a45518b7808/html5/thumbnails/9.jpg)
© 2010 Noah Mendelsohn
The server is identified by DNS name in the URI
HTTP GET
Host: webarch.noahdemo.com
URI is http://webarch.noahdemo.com/demo1/test.html URI is http://webarch.noahdemo.com/demo1/test.html
![Page 10: HTTP & REST - Tufts University · GET /demo1.html HTTP/1.0 . Host: webarch.noahdemo.com . User-Agent: ... In practice: – Cookies stored in browser tied to session state and login](https://reader031.fdocuments.in/reader031/viewer/2022021821/5b09f3247f8b9a45518b7808/html5/thumbnails/10.jpg)
© 2010 Noah Mendelsohn
HTTP GET
Host: webarch.noahdemo.com
GET /demo1/test.html HTTP/1.0 Host: webarch.noahdemo.com User-Agent: Noahs Demo HttpClient v1.0 Accept: */* Accept-language: en-us
URI is http://webarch.noahdemo.com/demo1/test.html
demo1/test.html
The client sends an HTTP GET
![Page 11: HTTP & REST - Tufts University · GET /demo1.html HTTP/1.0 . Host: webarch.noahdemo.com . User-Agent: ... In practice: – Cookies stored in browser tied to session state and login](https://reader031.fdocuments.in/reader031/viewer/2022021821/5b09f3247f8b9a45518b7808/html5/thumbnails/11.jpg)
© 2010 Noah Mendelsohn
HTTP GET
Host: webarch.noahdemo.com
GET /demo1/test.html HTTP/1.0 Host: webarch.noahdemo.com User-Agent: Noahs Demo HttpClient v1.0 Accept: */* Accept-language: en-us
URI is http://webarch.noahdemo.com/demo1/test.html
demo1/test.html
The client sends an HTTP GET
Note that HTTP is a text-based
protocol
…but it can carry binary entity
bodies such as image/jpeg
![Page 12: HTTP & REST - Tufts University · GET /demo1.html HTTP/1.0 . Host: webarch.noahdemo.com . User-Agent: ... In practice: – Cookies stored in browser tied to session state and login](https://reader031.fdocuments.in/reader031/viewer/2022021821/5b09f3247f8b9a45518b7808/html5/thumbnails/12.jpg)
© 2010 Noah Mendelsohn
The server sends an HTTP Response HTTP GET
HTTP RESPONSE
Host: webarch.noahdemo.com
demo1/test.html
HTTP/1.1 200 OK Date: Tue, 28 Aug 2007 01:49:33 GMT Server: Apache Transfer-Encoding: chunked Content-Type: text/html <html> <head> <title>Demo #1</title> </head> <body> <h1>A very simple Web page</h1> </body> </html>
HTTP Status Code 200
Means Success!
![Page 13: HTTP & REST - Tufts University · GET /demo1.html HTTP/1.0 . Host: webarch.noahdemo.com . User-Agent: ... In practice: – Cookies stored in browser tied to session state and login](https://reader031.fdocuments.in/reader031/viewer/2022021821/5b09f3247f8b9a45518b7808/html5/thumbnails/13.jpg)
© 2010 Noah Mendelsohn
The server sends an HTTP Response HTTP GET
HTTP RESPONSE
Host: webarch.noahdemo.com
demo1/test.html
HTTP/1.1 200 OK Date: Tue, 28 Aug 2007 01:49:33 GMT Server: Apache Transfer-Encoding: chunked Content-Type: text/html <html> <head> <title>Demo #1</title> </head> <body> <h1>A very simple Web page</h1> </body> </html>
The “representation” returned is an HTML document
![Page 14: HTTP & REST - Tufts University · GET /demo1.html HTTP/1.0 . Host: webarch.noahdemo.com . User-Agent: ... In practice: – Cookies stored in browser tied to session state and login](https://reader031.fdocuments.in/reader031/viewer/2022021821/5b09f3247f8b9a45518b7808/html5/thumbnails/14.jpg)
© 2010 Noah Mendelsohn
Host: webarch.noahdemo.com
demo1/test.html
The server sends an HTTP Response HTTP GET
HTTP RESPONSE
HTTP/1.1 200 OK Date: Tue, 28 Aug 2007 01:49:33 GMT Server: Apache Transfer-Encoding: chunked Content-Type: text/html <html> <head> <title>Demo #1</title> </head> <body> <h1>A very simple Web page</h1> </body> </html>
The HTML for the page.
![Page 15: HTTP & REST - Tufts University · GET /demo1.html HTTP/1.0 . Host: webarch.noahdemo.com . User-Agent: ... In practice: – Cookies stored in browser tied to session state and login](https://reader031.fdocuments.in/reader031/viewer/2022021821/5b09f3247f8b9a45518b7808/html5/thumbnails/15.jpg)
© 2010 Noah Mendelsohn
HTTP Methods, Headers &
Extensibility
![Page 16: HTTP & REST - Tufts University · GET /demo1.html HTTP/1.0 . Host: webarch.noahdemo.com . User-Agent: ... In practice: – Cookies stored in browser tied to session state and login](https://reader031.fdocuments.in/reader031/viewer/2022021821/5b09f3247f8b9a45518b7808/html5/thumbnails/16.jpg)
© 2010 Noah Mendelsohn
HTTP Requests always have a method
HTTP REQUEST
Host: webarch.noahdemo.com
GET /demo1.html HTTP/1.0 Host: webarch.noahdemo.com User-Agent: Noahs Demo HttpClient v1.0 Accept: */* Accept-language: en-us
URI is http://webarch.noahdemo.com/demo1/test.html
GET
![Page 17: HTTP & REST - Tufts University · GET /demo1.html HTTP/1.0 . Host: webarch.noahdemo.com . User-Agent: ... In practice: – Cookies stored in browser tied to session state and login](https://reader031.fdocuments.in/reader031/viewer/2022021821/5b09f3247f8b9a45518b7808/html5/thumbnails/17.jpg)
© 2010 Noah Mendelsohn
HTTP Headers
GET /demo1.html HTTP/1.0 Host: webarch.noahdemo.com User-Agent: Noahs Demo HttpClient v1.0 Accept: */* Accept-language: en-us
Request headers
HTTP/1.1 200 OK Date: Tue, 28 Aug 2007 01:49:33 GMT Server: Apache Transfer-Encoding: chunked Content-Type: text/html <!DOCTYPE html> <html> <head> <title>Demo #1</title> </head> <body> <h1>A very simple Web page</h1> </body> </html>
Response headers
![Page 18: HTTP & REST - Tufts University · GET /demo1.html HTTP/1.0 . Host: webarch.noahdemo.com . User-Agent: ... In practice: – Cookies stored in browser tied to session state and login](https://reader031.fdocuments.in/reader031/viewer/2022021821/5b09f3247f8b9a45518b7808/html5/thumbnails/18.jpg)
© 2010 Noah Mendelsohn
HTTP Extension Points
HTTP – New headers (quite common)
– New methods (rare)
– New versions of HTTP itself (e.g. HTTP 1.1, HTTP/2) (very rare)
– New status codes (very rare)
Technologies used by HTTP – Media types (text/html, image/jpec, application/…something new?...
– Languages (e.g. en-us)
– Transfer encodings (e.g. gzip)
![Page 19: HTTP & REST - Tufts University · GET /demo1.html HTTP/1.0 . Host: webarch.noahdemo.com . User-Agent: ... In practice: – Cookies stored in browser tied to session state and login](https://reader031.fdocuments.in/reader031/viewer/2022021821/5b09f3247f8b9a45518b7808/html5/thumbnails/19.jpg)
© 2010 Noah Mendelsohn
HTTP Protocol for a Discoverable Web
![Page 20: HTTP & REST - Tufts University · GET /demo1.html HTTP/1.0 . Host: webarch.noahdemo.com . User-Agent: ... In practice: – Cookies stored in browser tied to session state and login](https://reader031.fdocuments.in/reader031/viewer/2022021821/5b09f3247f8b9a45518b7808/html5/thumbnails/20.jpg)
© 2010 Noah Mendelsohn
HTTP: the protocol for a discoverable Web
HTTP provides a uniform interface to all resources You don’t have to know in advance what a resource is like
to access it with HTTP…so, you or your software can explore the Web in ad-hoc ways Self-describing: when a response comes back, you can
figure out what it means Using HTTP to access a resource doesn’t damage the
resource For all this to work, you must use HTTP…and you must use
it properly!
![Page 21: HTTP & REST - Tufts University · GET /demo1.html HTTP/1.0 . Host: webarch.noahdemo.com . User-Agent: ... In practice: – Cookies stored in browser tied to session state and login](https://reader031.fdocuments.in/reader031/viewer/2022021821/5b09f3247f8b9a45518b7808/html5/thumbnails/21.jpg)
© 2010 Noah Mendelsohn
HTTP Requests always have a method
HTTP REQUEST
Host: webarch.noahdemo.com
GET /demo1.html HTTP/1.0 Host: webarch.noahdemo.com User-Agent: Noahs Demo HttpClient v1.0 Accept: */* Accept-language: en-us
URI is http://webarch.noahdemo.com/demo1/test.html
GET
![Page 22: HTTP & REST - Tufts University · GET /demo1.html HTTP/1.0 . Host: webarch.noahdemo.com . User-Agent: ... In practice: – Cookies stored in browser tied to session state and login](https://reader031.fdocuments.in/reader031/viewer/2022021821/5b09f3247f8b9a45518b7808/html5/thumbnails/22.jpg)
© 2010 Noah Mendelsohn
HTTP Methods
Method Meaning GET Retrieve a representation of the resource but
do not change anything!
POST Update the resource or a dependent resource base on supplied representation
HEAD Optimized GET – retrieves only headers, used for checking caches, etc.
PUT Create or completely replace the resource base on supplied representation
DELETE Delete the resource identified by the URI
GET is very carefully defined to make the
Web scalable and discoverable.
![Page 23: HTTP & REST - Tufts University · GET /demo1.html HTTP/1.0 . Host: webarch.noahdemo.com . User-Agent: ... In practice: – Cookies stored in browser tied to session state and login](https://reader031.fdocuments.in/reader031/viewer/2022021821/5b09f3247f8b9a45518b7808/html5/thumbnails/23.jpg)
© 2010 Noah Mendelsohn
HTTP Methods
Method Meaning GET Retrieve a representation of the resource but
do not change anything!
POST Update the resource or a dependent resource base on supplied representation
HEAD Optimized GET – retrieves only headers, used for checking caches, etc.
PUT Create or completely replace the resource base on supplied representation
DELETE Delete the resource identified by the URI
GET is very carefully defined to make the
Web scalable and discoverable.
![Page 24: HTTP & REST - Tufts University · GET /demo1.html HTTP/1.0 . Host: webarch.noahdemo.com . User-Agent: ... In practice: – Cookies stored in browser tied to session state and login](https://reader031.fdocuments.in/reader031/viewer/2022021821/5b09f3247f8b9a45518b7808/html5/thumbnails/24.jpg)
© 2010 Noah Mendelsohn
HTTP GET Has A Very Particular Meaning
HTTP GET
Host: webarch.noahdemo.com
GET /demo1.html HTTP/1.0 Host: webarch.noahdemo.com User-Agent: Noahs Demo HttpClient v1.0 Accept: */* Accept-language: en-us
URI is http://webarch.noahdemo.com/demo1/test.html
GET
It’s always safe to try a GET on any URI. In fact, that’s why search engines like Google can safely crawl the Web!
![Page 25: HTTP & REST - Tufts University · GET /demo1.html HTTP/1.0 . Host: webarch.noahdemo.com . User-Agent: ... In practice: – Cookies stored in browser tied to session state and login](https://reader031.fdocuments.in/reader031/viewer/2022021821/5b09f3247f8b9a45518b7808/html5/thumbnails/25.jpg)
© 2010 Noah Mendelsohn
It’s always safe to try a GET on any URI. In fact, that’s why search engines like Google can safely crawl the Web!
But GET is so convenient that some people use it in the wrong places…if you’re changing the state of a resource you must use PUT or POST!
HTTP GET Has A Very Particular Meaning
![Page 26: HTTP & REST - Tufts University · GET /demo1.html HTTP/1.0 . Host: webarch.noahdemo.com . User-Agent: ... In practice: – Cookies stored in browser tied to session state and login](https://reader031.fdocuments.in/reader031/viewer/2022021821/5b09f3247f8b9a45518b7808/html5/thumbnails/26.jpg)
© 2010 Noah Mendelsohn
Demonstration: http://webarch.noahdemo.com/MisuseGet/
![Page 27: HTTP & REST - Tufts University · GET /demo1.html HTTP/1.0 . Host: webarch.noahdemo.com . User-Agent: ... In practice: – Cookies stored in browser tied to session state and login](https://reader031.fdocuments.in/reader031/viewer/2022021821/5b09f3247f8b9a45518b7808/html5/thumbnails/27.jpg)
© 2010 Noah Mendelsohn
Using HTTP Properly If GET is used instead of POST, your applications may
update accidentally
If too many applications misuse GET, then it will become impossible to explore the web – If you click a link, you might break something..you would have to ask about
each link before clicking!
– Search crawlers couldn’t be used…there would be no Google!
Read TAG Finding “URIs, Addressability, and the use of HTTP GET and
POST” http://www.w3.org/2001/tag/doc/whenToUseGet.html
![Page 28: HTTP & REST - Tufts University · GET /demo1.html HTTP/1.0 . Host: webarch.noahdemo.com . User-Agent: ... In practice: – Cookies stored in browser tied to session state and login](https://reader031.fdocuments.in/reader031/viewer/2022021821/5b09f3247f8b9a45518b7808/html5/thumbnails/28.jpg)
© 2010 Noah Mendelsohn
Web Interaction: Highlights
HTTP: a uniform way to interact with resources
Promotes interoperability & discoverablility
Use HTTP properly: GET is special!
HTTP has features like content negotiation and headers that let you adapt content for your users
![Page 29: HTTP & REST - Tufts University · GET /demo1.html HTTP/1.0 . Host: webarch.noahdemo.com . User-Agent: ... In practice: – Cookies stored in browser tied to session state and login](https://reader031.fdocuments.in/reader031/viewer/2022021821/5b09f3247f8b9a45518b7808/html5/thumbnails/29.jpg)
© 2010 Noah Mendelsohn
The Self-Describing Web
![Page 30: HTTP & REST - Tufts University · GET /demo1.html HTTP/1.0 . Host: webarch.noahdemo.com . User-Agent: ... In practice: – Cookies stored in browser tied to session state and login](https://reader031.fdocuments.in/reader031/viewer/2022021821/5b09f3247f8b9a45518b7808/html5/thumbnails/30.jpg)
© 2010 Noah Mendelsohn
The Self-Describing Web
Everything you need to understand a response is in the response…
…to find the instructions, read RFC 3989…
…and follow your nose!
![Page 31: HTTP & REST - Tufts University · GET /demo1.html HTTP/1.0 . Host: webarch.noahdemo.com . User-Agent: ... In practice: – Cookies stored in browser tied to session state and login](https://reader031.fdocuments.in/reader031/viewer/2022021821/5b09f3247f8b9a45518b7808/html5/thumbnails/31.jpg)
© 2010 Noah Mendelsohn
Follow your nose
The Web community’s term for: Going step by step through a Web response and the pertinent
specifications… …to figure out what the response means All the specifications you need are found (transitively) from RFC 3986! Thus there is a quite rigorous sense in which the Web is browsable and
explorable
Read TAG Finding “The Self-Describing Web”
http://www.w3.org/2001/tag/doc/selfDescribingDocuments
![Page 32: HTTP & REST - Tufts University · GET /demo1.html HTTP/1.0 . Host: webarch.noahdemo.com . User-Agent: ... In practice: – Cookies stored in browser tied to session state and login](https://reader031.fdocuments.in/reader031/viewer/2022021821/5b09f3247f8b9a45518b7808/html5/thumbnails/32.jpg)
© 2010 Noah Mendelsohn
The server sends an HTTP Response HTTP GET
HTTP RESPONSE
Host: webarch.noahdemo.com
demo1/test.html
HTTP/1.1 200 OK Date: Tue, 28 Aug 2007 01:49:33 GMT Server: Apache Transfer-Encoding: chunked Content-Type: text/html <html> <head> <title>Demo #1</title> </head> <body> <h1>A very simple Web page</h1> </body> </html>
The “representation” returned is an HTML document
![Page 33: HTTP & REST - Tufts University · GET /demo1.html HTTP/1.0 . Host: webarch.noahdemo.com . User-Agent: ... In practice: – Cookies stored in browser tied to session state and login](https://reader031.fdocuments.in/reader031/viewer/2022021821/5b09f3247f8b9a45518b7808/html5/thumbnails/33.jpg)
© 2010 Noah Mendelsohn
Stateful and Stateless Protocols
![Page 34: HTTP & REST - Tufts University · GET /demo1.html HTTP/1.0 . Host: webarch.noahdemo.com . User-Agent: ... In practice: – Cookies stored in browser tied to session state and login](https://reader031.fdocuments.in/reader031/viewer/2022021821/5b09f3247f8b9a45518b7808/html5/thumbnails/34.jpg)
© 2010 Noah Mendelsohn
Stateful and Stateless Protocols (review)
Stateful: server knows which step (state) has been reached
Stateless: – Client remembers the state, sends to server each time
– Server processes each request independently
Can vary with level – Many systems like Web run stateless protocols (e.g. HTTP) over
streams…at the packet level, TCP streams are stateful
– HTTP itself is mostly stateless, but many HTTP requests (typically POSTs) update persistent state at the server
![Page 35: HTTP & REST - Tufts University · GET /demo1.html HTTP/1.0 . Host: webarch.noahdemo.com . User-Agent: ... In practice: – Cookies stored in browser tied to session state and login](https://reader031.fdocuments.in/reader031/viewer/2022021821/5b09f3247f8b9a45518b7808/html5/thumbnails/35.jpg)
© 2010 Noah Mendelsohn
Advantages of stateless protocols
Protocol usually simpler
Server processes each request independently
Load balancing and restart easier
Typically easier to scale and make fault-tolerant
Visibility: individual requests more self-describing
![Page 36: HTTP & REST - Tufts University · GET /demo1.html HTTP/1.0 . Host: webarch.noahdemo.com . User-Agent: ... In practice: – Cookies stored in browser tied to session state and login](https://reader031.fdocuments.in/reader031/viewer/2022021821/5b09f3247f8b9a45518b7808/html5/thumbnails/36.jpg)
© 2010 Noah Mendelsohn
Advantages of stateful protocols
Individual messages carry less data
Server does not have to re-establish context each time
There’s usually some changing state at the server at some level, except for completely static publishing systems
![Page 37: HTTP & REST - Tufts University · GET /demo1.html HTTP/1.0 . Host: webarch.noahdemo.com . User-Agent: ... In practice: – Cookies stored in browser tied to session state and login](https://reader031.fdocuments.in/reader031/viewer/2022021821/5b09f3247f8b9a45518b7808/html5/thumbnails/37.jpg)
© 2010 Noah Mendelsohn
HTTP is Stateless (usually)
HTTP is fundamentally stateless…response determined entirely from GET request & URI – (unless resource itself changes!!)
In practice: – Cookies stored in browser tied to session state and login status
– OK: determine access rights from cookie
– NOT OK: return different content based on cookie
– WHY?: On the Web, we identify with URIs, not cookies!
Of course, the state of resources does change: – E.g. when we add something to a shopping cart
38
![Page 38: HTTP & REST - Tufts University · GET /demo1.html HTTP/1.0 . Host: webarch.noahdemo.com . User-Agent: ... In practice: – Cookies stored in browser tied to session state and login](https://reader031.fdocuments.in/reader031/viewer/2022021821/5b09f3247f8b9a45518b7808/html5/thumbnails/38.jpg)
© 2010 Noah Mendelsohn
REST
![Page 39: HTTP & REST - Tufts University · GET /demo1.html HTTP/1.0 . Host: webarch.noahdemo.com . User-Agent: ... In practice: – Cookies stored in browser tied to session state and login](https://reader031.fdocuments.in/reader031/viewer/2022021821/5b09f3247f8b9a45518b7808/html5/thumbnails/39.jpg)
© 2010 Noah Mendelsohn
REST – REpresenational State Transfer
What is REST? – A distributed system architecture
– In practice: implemented using HTTP
Key features – States transferred using representations (HTTP requests/responses)
– Server is stateless
– URIs (and request data if provided) convey state to application
History – Described by Roy Fielding in his PhD thesis
– Came after Tim BL invented the Web, but…
– …Roy was very influential in development of formal specifications of and details of HTTP and URIs
Fielding’s PhD Thesis: http://www.ics.uci.edu/~fielding/pubs/dissertation/fielding_dissertation.pdf
![Page 40: HTTP & REST - Tufts University · GET /demo1.html HTTP/1.0 . Host: webarch.noahdemo.com . User-Agent: ... In practice: – Cookies stored in browser tied to session state and login](https://reader031.fdocuments.in/reader031/viewer/2022021821/5b09f3247f8b9a45518b7808/html5/thumbnails/40.jpg)
© 2010 Noah Mendelsohn
Building a REST application
All data communicated using HTTP GET / POST – Unit of interaction is document – Formats like JSON, XML, RDF, N3 used to convey data
State of navigation typically captured in URIs: – URI to start application – URI for 2nd step – URI for 3rd step – Captures client parameters & input in URIs (e.g. search parms, flight numbers)
States of a REST application are linkable, bookmarkable Examples:
– http://webarch.noahdemo.com/MisuseGet/ – Google search – Google maps (AJAX example: uses REST recursively)
![Page 41: HTTP & REST - Tufts University · GET /demo1.html HTTP/1.0 . Host: webarch.noahdemo.com . User-Agent: ... In practice: – Cookies stored in browser tied to session state and login](https://reader031.fdocuments.in/reader031/viewer/2022021821/5b09f3247f8b9a45518b7808/html5/thumbnails/41.jpg)
© 2010 Noah Mendelsohn
Demonstration: http://www.webarch.noahdemo.com/UseUris/
![Page 42: HTTP & REST - Tufts University · GET /demo1.html HTTP/1.0 . Host: webarch.noahdemo.com . User-Agent: ... In practice: – Cookies stored in browser tied to session state and login](https://reader031.fdocuments.in/reader031/viewer/2022021821/5b09f3247f8b9a45518b7808/html5/thumbnails/42.jpg)
© 2010 Noah Mendelsohn
A Complex REST Application Google Maps
Warning: some details of Google’s implementation may have changed since this
presentation was prepared
![Page 43: HTTP & REST - Tufts University · GET /demo1.html HTTP/1.0 . Host: webarch.noahdemo.com . User-Agent: ... In practice: – Cookies stored in browser tied to session state and login](https://reader031.fdocuments.in/reader031/viewer/2022021821/5b09f3247f8b9a45518b7808/html5/thumbnails/43.jpg)
© 2010 Noah Mendelsohn
AJAX Application – Google Maps
Images retrieved in segments using ordinary Web HTTP Requests
![Page 44: HTTP & REST - Tufts University · GET /demo1.html HTTP/1.0 . Host: webarch.noahdemo.com . User-Agent: ... In practice: – Cookies stored in browser tied to session state and login](https://reader031.fdocuments.in/reader031/viewer/2022021821/5b09f3247f8b9a45518b7808/html5/thumbnails/44.jpg)
© 2010 Noah Mendelsohn
AJAX Application – Google Maps
JavaScript at client tracks mouse and moves images for smooth panning…
asynchronously requests new image tiles in background
![Page 45: HTTP & REST - Tufts University · GET /demo1.html HTTP/1.0 . Host: webarch.noahdemo.com . User-Agent: ... In practice: – Cookies stored in browser tied to session state and login](https://reader031.fdocuments.in/reader031/viewer/2022021821/5b09f3247f8b9a45518b7808/html5/thumbnails/45.jpg)
© 2010 Noah Mendelsohn
AJAX Application – Google Maps
The Web is used to retrieve an ordinary file listing points of interest…. (used to be XML
but could use JSON, other?)
<?xml version="1.0" ?> <page> <title>hotels in hawthorne</title> <query>pizza in atlanta</query> <center lat="33.748888" lng="-84.388056" /> <info> <title>Wellesley Inn</title> <address> <line>540 Saw Mill River Rd.</line> <line>Elmsford, NY 10523</line> </address> </page>
![Page 46: HTTP & REST - Tufts University · GET /demo1.html HTTP/1.0 . Host: webarch.noahdemo.com . User-Agent: ... In practice: – Cookies stored in browser tied to session state and login](https://reader031.fdocuments.in/reader031/viewer/2022021821/5b09f3247f8b9a45518b7808/html5/thumbnails/46.jpg)
© 2010 Noah Mendelsohn
AJAX Application – Google Maps
…Data used to drive formatting of points of interest
<?xml version="1.0" ?> <page> <title>hotels in hawthorne</title> <query>pizza in atlanta</query> <center lat="33.748888" lng="-84.388056" /> <info> <title>Wellesley Inn</title> <address> <line>540 Saw Mill River Rd.</line> <line>Elmsford, NY 10523</line> </address> </page>
![Page 47: HTTP & REST - Tufts University · GET /demo1.html HTTP/1.0 . Host: webarch.noahdemo.com . User-Agent: ... In practice: – Cookies stored in browser tied to session state and login](https://reader031.fdocuments.in/reader031/viewer/2022021821/5b09f3247f8b9a45518b7808/html5/thumbnails/47.jpg)
© 2010 Noah Mendelsohn
AJAX Application – Google Maps
…and XSLT in the browser converts that to HTML
<?xml version="1.0" ?> <page> <title>hotels in hawthorne</title> <query>pizza in atlanta</query> <center lat="33.748888" lng="-84.388056" /> <info> <title>Wellesley Inn</title> <address> <line>540 Saw Mill River Rd.</line> <line>Elmsford, NY 10523</line> </address> </page>
![Page 48: HTTP & REST - Tufts University · GET /demo1.html HTTP/1.0 . Host: webarch.noahdemo.com . User-Agent: ... In practice: – Cookies stored in browser tied to session state and login](https://reader031.fdocuments.in/reader031/viewer/2022021821/5b09f3247f8b9a45518b7808/html5/thumbnails/48.jpg)
© 2010 Noah Mendelsohn
AJAX Application – Google Maps
![Page 49: HTTP & REST - Tufts University · GET /demo1.html HTTP/1.0 . Host: webarch.noahdemo.com . User-Agent: ... In practice: – Cookies stored in browser tied to session state and login](https://reader031.fdocuments.in/reader031/viewer/2022021821/5b09f3247f8b9a45518b7808/html5/thumbnails/49.jpg)
© 2010 Noah Mendelsohn
Question: What would happen if we built the
Web from RPC? ---
Revisited!
![Page 50: HTTP & REST - Tufts University · GET /demo1.html HTTP/1.0 . Host: webarch.noahdemo.com . User-Agent: ... In practice: – Cookies stored in browser tied to session state and login](https://reader031.fdocuments.in/reader031/viewer/2022021821/5b09f3247f8b9a45518b7808/html5/thumbnails/50.jpg)
© 2010 Noah Mendelsohn
Why not use RPC for the Web?
No uniform interface – You’d need an interface definition to follow a link
No global naming
Most RPC data types poorly tuned for documents
![Page 51: HTTP & REST - Tufts University · GET /demo1.html HTTP/1.0 . Host: webarch.noahdemo.com . User-Agent: ... In practice: – Cookies stored in browser tied to session state and login](https://reader031.fdocuments.in/reader031/viewer/2022021821/5b09f3247f8b9a45518b7808/html5/thumbnails/51.jpg)
© 2010 Noah Mendelsohn
Why not use RPC for the Web?
No uniform interface – You’d need an interface definition to follow a link
No global naming
Most RPC data types poorly tuned for documents
No safe methods
![Page 52: HTTP & REST - Tufts University · GET /demo1.html HTTP/1.0 . Host: webarch.noahdemo.com . User-Agent: ... In practice: – Cookies stored in browser tied to session state and login](https://reader031.fdocuments.in/reader031/viewer/2022021821/5b09f3247f8b9a45518b7808/html5/thumbnails/52.jpg)
© 2010 Noah Mendelsohn
Why not use RPC for the Web?
No uniform interface – You’d need an interface definition to follow a link
No global naming
Most RPC data types poorly tuned for documents
No safe methods
No content negotiation
![Page 53: HTTP & REST - Tufts University · GET /demo1.html HTTP/1.0 . Host: webarch.noahdemo.com . User-Agent: ... In practice: – Cookies stored in browser tied to session state and login](https://reader031.fdocuments.in/reader031/viewer/2022021821/5b09f3247f8b9a45518b7808/html5/thumbnails/53.jpg)
© 2010 Noah Mendelsohn
HTTP Content Negotiation
![Page 54: HTTP & REST - Tufts University · GET /demo1.html HTTP/1.0 . Host: webarch.noahdemo.com . User-Agent: ... In practice: – Cookies stored in browser tied to session state and login](https://reader031.fdocuments.in/reader031/viewer/2022021821/5b09f3247f8b9a45518b7808/html5/thumbnails/54.jpg)
© 2010 Noah Mendelsohn
Content negotiation for language
HTTP GET
Host: webarch.noahdemo.com
GET /demo1/test.html HTTP/1.0 Host: webarch.noahdemo.com User-Agent: Noahs Demo HttpClient v1.0 Accept: */* Accept-language: en-us
URI is http://webarch.noahdemo.com/demo1/test.html
Accept-language: en-us
I prefer English, please!
But: it is OK to also provide individual URIs for each language version
![Page 55: HTTP & REST - Tufts University · GET /demo1.html HTTP/1.0 . Host: webarch.noahdemo.com . User-Agent: ... In practice: – Cookies stored in browser tied to session state and login](https://reader031.fdocuments.in/reader031/viewer/2022021821/5b09f3247f8b9a45518b7808/html5/thumbnails/55.jpg)
© 2010 Noah Mendelsohn
Content negotiation for device
HTTP GET
Host: webarch.noahdemo.com
URI is http://webarch.noahdemo.com/demo1/test.html
GET /demo1/test.html HTTP/1.0 Host: webarch.noahdemo.com User-Agent: Noahs Demo HttpClient v1.0 Accept: */* Accept-language: en-us
User-Agent: Mozilla/4.0
Firefox
![Page 56: HTTP & REST - Tufts University · GET /demo1.html HTTP/1.0 . Host: webarch.noahdemo.com . User-Agent: ... In practice: – Cookies stored in browser tied to session state and login](https://reader031.fdocuments.in/reader031/viewer/2022021821/5b09f3247f8b9a45518b7808/html5/thumbnails/56.jpg)
© 2010 Noah Mendelsohn
Content negotiation for device
Host: webarch.noahdemo.com
GET /demo1/test.html HTTP/1.0 Host: webarch.noahdemo.com User-Agent: Noahs Demo HttpClient v1.0 Accept: */* Accept-language: en-us
URI is http://webarch.noahdemo.com/demo1/test.html
User-Agent: Cell Phone Browser V1.1
Cell phone browser
HTTP GET
![Page 57: HTTP & REST - Tufts University · GET /demo1.html HTTP/1.0 . Host: webarch.noahdemo.com . User-Agent: ... In practice: – Cookies stored in browser tied to session state and login](https://reader031.fdocuments.in/reader031/viewer/2022021821/5b09f3247f8b9a45518b7808/html5/thumbnails/57.jpg)
© 2010 Noah Mendelsohn
Device independence The same URI works across devices…
…you can see my reservation from your cell phone
Even phone numbers are on the Web
<a href=“tel:18005551212”>
![Page 58: HTTP & REST - Tufts University · GET /demo1.html HTTP/1.0 . Host: webarch.noahdemo.com . User-Agent: ... In practice: – Cookies stored in browser tied to session state and login](https://reader031.fdocuments.in/reader031/viewer/2022021821/5b09f3247f8b9a45518b7808/html5/thumbnails/58.jpg)
© 2010 Noah Mendelsohn
Summary
![Page 59: HTTP & REST - Tufts University · GET /demo1.html HTTP/1.0 . Host: webarch.noahdemo.com . User-Agent: ... In practice: – Cookies stored in browser tied to session state and login](https://reader031.fdocuments.in/reader031/viewer/2022021821/5b09f3247f8b9a45518b7808/html5/thumbnails/59.jpg)
© 2010 Noah Mendelsohn
Summary
HTTP and the REST model are designed to meet the unique needs of the Web
Document-oriented
Discoverable
Stateless
Extensible
Global scale
Supports unique features like content negotiation