CS3 Rest
CS3 Rest
Akshaya Ganesan
Assistant Professor[Off-Campus]
BITS-Pilani
Precap
Contact Session 2
• Service Terminology- Service, Service capability, Service contract, composition
• Service Models and Layers- Utility, Task, Entity
• Service-Related Granularity- Service, capability, Data, Constraint
• Service Orientation Design principles
• Service Orientation Goals and Benefits
• ESB and SOA
Agenda
• When a developer calls Instagram API to fetch a specific user (the resource)
the API will return the state of that user, including
their name
the number of posts that user posted on Instagram so far
how many followers they have
and more
• Server needs to know about 2 things, based upon which it takes some action and return the
response
An identifier for the resource you are interested in
URL for the resource, also known as the endpoint
URL stands for Uniform Resource Locator
The operation you want the server to perform on that resource
in the form of an HTTP method, or verb
common HTTP methods are GET, POST, PUT, and DELETE
• For example,
fetching a specific Twitter user, using Twitter’s RESTful API, will require a URL that identify that user
and the HTTP method GET
Twitter URL: www.twitter.com/jk_rowling has the unique identifier for J. K. Rowling’s Twitter user, which
is her username, jk_rowling
Twitter uses the username as the identifier
The HTTP method GET indicates that we want to get the state of that user
REST and HTTP
REST is an architectural style and not a programming language or technology
• The fundamental principle of REST is to use the HTTP protocol for data communication.
• RESTful web service makes use of HTTP for determining the action to be carried out on the particular
resources
• REST gets its motivation from HTTP, therefore, it can be said as structural pillar of the REST
• HTTP is a document-based protocol, in which the client puts a document in an envelope and sends it
to the server.
• The server returns the favor by putting a response document in an envelope and sending it to the
client.
Basic HTTP features for REST
An HTTP GET request for http://www.oreilly.com/index.html The response to an HTTP GET request for http://www.oreilly.com/index.html
GET /index.html HTTP/1.1 HTTP/1.1 200 OK
Date: Fri, 17 Nov 2006 15:36:32 GMT
Host: www.oreilly.com
Server: Apache
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.12)... Last-Modified: Fri, 17 Nov 2006 09:05:32 GMT
Accept: Etag: "7359b7-a7fa-455d8264
text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,... Accept-Ranges: bytes
Accept-Language: us,en;q=0.5 Content-Length: 43302
Accept-Encoding: gzip,deflate Content-Type: text/html
Accept-Charset: ISO-8859-15,utf-8;q=0.7,*;q=0.7 X-Cache: MISS from www.oreilly.com
Keep-Alive: 300 Keep-Alive: timeout=15, max=1000
Connection: keep-alive Connection: Keep-Alive
• A RESTful, resource-oriented service exposes a URI for every piece of data the client might
want to operate on.
RESTful Webservices
• In RESTful web service, the method information goes into the HTTP method.
• The scoping information goes into the URI.
• Given the first line of an HTTP request to a RESTful web service
• (“GET /reports/open-bugs HTTP/1.1”),
• you should understand basically what the client wants to do.
• If the HTTP method doesn’t match the method information, the service isn’t RESTful.
• The service is not resource-oriented if the scoping information isn’t in the URI.
• http://www.upcdatabase.com/upc/00598491'
REST Constraints
• The client and the server act independently, each on its own
Interaction between them is only in the form of
requests initiated by the client only
responses, which the server send to the client only as a reaction to a request
• The server just sits there waiting for requests from the client to come
doesn’t start sending away information about the state of some resources on its own
Responds only when request comes in
Stateless
The statelessness constraint helps services to be more scalable and reliable.
• Stateless means the server does not remember anything about the user who uses the service
doesn’t remember if the user already sent a GET request for the same resource in the past
doesn’t remember which resources the user of the API requested before
and so on...
• Each individual request contains all the information the server needs to perform the request and
return a response, regardless of other requests made by the same user.
• The client is responsible for sending any state information to the server whenever it's needed.
• No session stickiness or session affinity on the server for the calling request
Cacheable
• client knows which version of the data it already has (from a previous response)
the client can avoid requesting the same data again and again
• client should also know if the current version of the data is expired,
will know it should send another request to the server to get the most updated data about the state of a
resource
Uniform interface
The intention of a uniform interface is to retain some common vocabulary across the internet.
• There are four guiding principles suggested by Fielding that constitute the necessary constraints to
satisfy the uniform interface
• Identification of resources
• Manipulation of resources
• Self-descriptive messages
• Hypermedia as the engine of application state
• The request to the server has to include a resource identifier
• Each request to the web service contains all the information the server needs to perform the request
Each response the server returns contain all the information the client needs to understand the response
• The response the server returns include enough information so the client can manipulate the
resource
• Hypermedia as the engine of application state
Application mean the web application that the server is running
Hypermedia mean the hyperlinks, or simply links, that the server can include in the response
means that the server can inform the client , in a response, of the ways to change the state of the web
application
HATEOS
Hypermedia as the Engine of Application State
Layered system
• Between the client who requests a representation of a resource’s state, and the server who
sends the response back
there might be a number of servers in the middle
servers might provide a security layer, a caching layer, a load-balancing layer etc.,
layers should not affect the request or the response
• The client is agnostic as to how many layers, if any, there are between the client and the actual
server responding to the request.
Code-on-demand (Optional)
• Is optional — a web service can be RESTful even without providing code on demand
Body
{
“Name” : “XYZ”,
“ Year” : “First”,
“DOB” : “25-04-1998”
}
Example - GET
GET – retrieve the resource
• Performance
• Scalability
• Simplicity
• Modifiability
• Visibility
• Portability
• Reliability
• Testability
References
1) Restful Web services, Leonard Richardson and Sam Ruby, 1 st edition published by O'Reilly
Media, May 2007 , Chapter 4, 8
Thank You!
In our next session: