HTTP && REST
HTTP && REST
What is Internet?
Internet
Network of computer systems which communicate with
each other to exchange information through standard set of
protocols.
curl http://httpbin.org/get -v
POST
Requests that the server accept the entity enclosed in the request as a new subordinate of the web
resource identified by the URI.
HTTP/1.1 200 OK
Date: Fri, 04 Jul 2014 11:04:20 GMT
Server: Apache/2.2.22 (Ubuntu)
Last-Modified: Fri, 26 Apr 2013 11:52:07 GMT
ETag: "1e20b4-b1-4db422a19aa97"
Accept-Ranges: bytes
Content-Length: 177
Vary: Accept-Encoding
Content-Type: text/html
X-Pad: avoid browser bug
<html><body><h1>It works!</h1>
<p>This is the default web page for this server.</p>
<p>The web server software is running but no content has been added, yet.</p>
</body></html>
HTTP METHODS
THATS IT RIGHT ???
YA RIGHT !!!
HEAD
Asks for the response identical to the one that would correspond to a GET request, but without the
response body. This is useful for retrieving meta-information written in response headers, without
having to transport the entire content.
curl http://httpbin.org/get -v -I
PUT
Requests that the enclosed entity be stored under the supplied URI. If the URI refers to an already
existing resource, it is modified; if the URI does not point to an existing resource, then the server can
create the resource with that URI. [Idempotent]
http://www.restapitutorial.com/httpstatuscodes.html
Headers
Operating parameters of transaction
● Common Request Headers
○ Host
○ Content-Type
○ Content-Length
○ User-Agent
● Common Response Headers
○ Date
○ Content-Type
○ Set-Cookie
○ Content-Encoding
curl -H "Accept-Encoding: gzip,deflate" "http://www.theverge.com/" -I -vN
Caching Headers
http://www.mobify.com/blog/beginners-guide-to
-http-cache-headers/
Caching Headers
● Reduces network traffic
● Reduces load time
● Cache expiry headers
○ Expires
○ Cache-Control: max-age (overrides Expires)
● There are only two hard things in Computer Science:
cache invalidation and naming things. (Phil Karlton)
● Resource version in URL
Conditional Headers
● Last-Modified
● ETag
● Conditional GET
○ If-Modified-Since
○ If-None-Match
○ If-Range
Cookies
● Small pieces of data stored on client and
sent on every request
● Use Cases:
○ Session management
○ Personalization
○ Tracking
Cookies
● Can be set both through headers and with JS
● Cookie attributes
○ Path
○ Expires
○ Secure
○ Domain
○ HttpOnly
● Set-Cookie Header
○ MyCookie=ChocoChip; Expires=Wed, 09 Jun 2021 10:18:14 GMT
● Authentication
● HTTPS - TLS Handshake
● Content Negotiation
○ Accept, Accept-Language
● Proxies
○ Forward & Reverse Proxies
● Optimizations???
● CDN …
Web API
● Allows access to data without scrapping
● HTTP + Definition of response format
● Allows multiple interfaces to your application
● SOA
● SOAP and REST
SOAP
● Simple Object Access Protocol
○ yeah very SIMPLE
● Defines an entire protocol on top of other
application protocols
● Supports only XML
● Reads cannot be cached
● Requires special tools to create a client
● Generally worse performance
REST
Representational state transfer. Defined by Roy Fielding in 2000. He
was also a principal author of HTTP 1.0 & 1.1
Wiki says :
REST is an architectural style consisting of a coordinated set of
architectural constraints applied to components, connectors, and data
elements, within a distributed hypermedia system. REST ignores the
details of component implementation and protocol syntax in order to
focus on the roles of components, the constraints upon their interaction
with other components, and their interpretation of significant data
elements
REST
http://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm
● 6 Constraints
○ Client-Server (Separation of concerns)
○ Stateless (state should be addressable by URL)
○ Cacheable
○ Layered system
○ Code on Demand (Optional)
○ Uniform Interface
Checkout:
https://speakerdeck.com/cprerovsky/rest-in-pea
ce-number-devoxxpl-2016-talk
https://dev.twitter.com/rest/reference
tips
1. Endpoint Naming - Use plurals GET /nodes
2. HTTP verbs - CRUD : POST-GET-PUT-DELETE
3. Subresources
4. Data formats : Decide for one output format
5. Models and Resources
6. Status codes
7. Path Parameters and resources
8. API versioning
9. State
10. Id’s
11. REST clients
RMM
http://www.infoq.com/news/2010/03/RESTLevels
More on Level 3
HATEOAS :
https://en.wikipedia.org/wiki/HATEOAS
Self Study :
http://www.slideshare.net/josdirksen/rest-from-g
et-to-hateoas
REST spec sheet design