Restful web services


Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Restful web services

  1. 1. RESTful Web Services
  2. 2. What are Web Services <ul><li>According to the Wikipedia - </li></ul><ul><ul><li>A web service is a method of communication between two </li></ul></ul><ul><ul><li>electronic devices over a network. </li></ul></ul><ul><li>The W3C defines a web service as a software system designed to support interoperable machine-to-machine interaction over a network. </li></ul><ul><li>Nowadays the web is full of web services: search engines, online stores, weblogs, wikis, calculators, games etc. and so it's full of data. Machines fetches these data from the programmable web to perform some tasks. </li></ul>
  3. 3. Programmable Web <ul><li>The programmable web is just as same as the Web that we, the humans, interacts with. </li></ul><ul><li>The main difference is that instead of arranging its data in attractive HTML pages with banner and ads and cute logos, the programmable web usually serves stark, brutal XML documents which are not necessarily for human consumption. It's data is intended as input to a software program that does some tasks. </li></ul><ul><li>At its best, the programmable web works the same way as the human web. The clients of it retrieves data from it and figure out what do they mean, and they can also modify the programmable web, just like the human web. </li></ul>
  4. 4. Kinds of Things on the Programmable Web <ul><li>The programmable web is based on HTTP and XML. Some parts of it serves HTML, JSON, plain text or binary documents, but most parts use XML, and it's all based on HTTP. </li></ul><ul><li>There are basically two ways of classifying the services that inhabit the programmable web: by the technologies they use (URIs, SOAP, XML-RPC and so on), or by the underlying architectures and design philosophies. </li></ul><ul><li>Most of today's terminology sorts services by their superficial appearances: the technology they use. These classifications work in most cases, but they're conceptually lacking and lead to mistakes. It would be better if a taxonomy based on architecture is used, which shows how technology choices follow from underlying design principles. </li></ul>
  5. 5. HTTP – The Common Thing of the Programmable Web <ul><li>To classify the programmable web, it's better to start off with an overview of HTTP, the protocol that all web services have in common. </li></ul><ul><li>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. The protocol defines a strict format for thie envelope, but it doesn't care what goes inside. </li></ul><ul><li>In HTTP terms, this envelope is called either a request or a response . When a client sends a request to a server, then it's called an HTTP Request, and when a response comes from the server, then this envelope is called an HTTP Response. </li></ul>
  6. 6. A Sample HTTP Request <ul><li>A sample HTTP request header is given below - </li></ul><ul><ul><li>GET /index.html HTTP/1.1 </li></ul></ul><ul><ul><li>Host: </li></ul></ul><ul><ul><li>User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.12)... </li></ul></ul><ul><ul><li>Accept:text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,... </li></ul></ul><ul><ul><li>Accept-Language: us,en;q=0.5 </li></ul></ul><ul><ul><li>Accept-Encoding: gzip,deflate </li></ul></ul><ul><ul><li>Accept-Charset: ISO-8859-15,utf-8;q=0.7,*;q=0.7 </li></ul></ul><ul><ul><li>Keep-Alive: 300 </li></ul></ul><ul><ul><li>Connection: keep-alive </li></ul></ul>
  7. 7. Parts of a HTTP request - The HTTP Method <ul><li>The name of the HTTP method is like a method name in a programming language: it indicates how the client expects the server to process this envelope. </li></ul><ul><li>In the sample example, the method is GET , which means the client is expecting the server to get some information for it. </li></ul><ul><li>The HTTP standard defines a total of 9 methods. They are - </li></ul><ul><ul><li>1. GET 6. TRACE </li></ul></ul><ul><ul><li>2. POST 7. OPTIONS </li></ul></ul><ul><ul><li>3. HEAD 8. CONNECT </li></ul></ul><ul><ul><li>4. PUT 9. PATCH ( rfc5789 ) </li></ul></ul><ul><ul><li>5. DELETE </li></ul></ul>
  8. 8. Parts of a HTTP request - The path <ul><li>This is the portion of the URI to the right of the hostname. In this example, it's /index.html. </li></ul><ul><li>In terms of the envelope metaphore, the path is the address on the envelope. </li></ul><ul><li>When combined with the hostname, it becomes something like , which uniquely specifies a resource on the web. </li></ul>
  9. 9. Parts of a HTTP request – The request headers <ul><li>These are bits of metadata: key-value pairs that act like informational stickers slapped onto the envelope. </li></ul><ul><li>Our sample example request has 8 headers: Host, User-Agent, Accept, and so on. </li></ul><ul><li>There's a standard list of HTTP headers, and applications can define their own. </li></ul>
  10. 10. Parts of a HTTP request – The entity-body <ul><li>It's also called the document or representation. </li></ul><ul><li>This is the document that is inside the envelope. This particular request has no entity-body, which means the envelope is empty. </li></ul><ul><li>This is typical for a GET request, where all the information needed to complete the request is in the path and the headers. </li></ul>
  11. 11. A Sample HTTP Response <ul><li>A sample HTTP response is given below - </li></ul><ul><ul><li>HTTP/1.1 200 OK </li></ul></ul><ul><ul><li>Date: Fri, 17 Nov 2006 15:36:32 GMT </li></ul></ul><ul><ul><li>Server: Apache </li></ul></ul><ul><ul><li>Last-Modified: Fri, 17 Nov 2006 09:05:32 GMT </li></ul></ul><ul><ul><li>Etag: &quot;7359b7-a7fa-455d8264 </li></ul></ul><ul><ul><li>Accept-Ranges: bytes </li></ul></ul><ul><ul><li>Content-Length: 43302 </li></ul></ul><ul><ul><li>Content-Type: text/html </li></ul></ul><ul><ul><li>X-Cache: MISS from </li></ul></ul><ul><ul><li>Keep-Alive: timeout=15, max=1000 </li></ul></ul><ul><ul><li>Connection: Keep-Alive </li></ul></ul><ul><ul><li><!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN ...... </li></ul></ul>
  12. 12. Parts of HTTP Response – HTTP Response Code <ul><li>It's a numeric code that tells the client whether the request went well or poorly, and how the client should regard this envelope and its contents. </li></ul><ul><li>In our example, the GET operation must have succeeded, since the response code is 200, which is interpreted as OK . </li></ul><ul><li>There are 5 categories of HTTP response code. They are - </li></ul><ul><ul><li>1xx: Informational </li></ul></ul><ul><ul><li>2xx: Success </li></ul></ul><ul><ul><li>3xx: Redirection </li></ul></ul><ul><ul><li>4xx: Client Error </li></ul></ul><ul><ul><li>5xx: Server Error </li></ul></ul>
  13. 13. Parts of HTTP Response – The entity-body and response headers <ul><li>Again, the entity-body is the document inside the envelope. This time, the request has an entity body, which is the fulfillment of the GET request. </li></ul><ul><li>The response headers are the same as the request headers – they are just informational stickers slapped onto the envelope. </li></ul><ul><li>Our example response has a total of 11 response headers: Date, Server and so on. </li></ul>
  14. 14. Differences between web services <ul><li>HTTP is the one thing that all services on the programmable web have in common. But there are two big questions that today's web services answer differently. </li></ul><ul><li>If we know how a web service answers these questions, then we will have a good idea of how well it works with the web </li></ul>
  15. 15. The First Question – Method Information <ul><li>The first burning question is – how the client can convey its intentions to the server? How does the server know a certain request is a request to retrieve some data, instead of a request to delete that same data or to overwrite it with different data? Why should the server do this instead of doing that? </li></ul><ul><li>This information about what to do with the data can be called the Method Information . </li></ul><ul><li>One way to convey method information in a web service is to put it in the HTTP method. This is how RESTful web services do it. </li></ul><ul><li>The great advantage of HTTP method names is that they're standardized. </li></ul>
  16. 16. <ul><li>Some web services keep method information in the URI path or the request document. </li></ul><ul><li>For example, let us consider the web service for Flickr. When someone sends HTTP requests to it's search API , the server searches for Photos. The HTTP method being used here is GET . </li></ul><ul><li>But the flickr supports many methods, not just GET -type methods such as, and so on. All of them are invoked with an HTTP GET request, regardless of whether or not they GET any data. So practically Flickr is sticking its method information in the URI. Similarly, SOAP services also don't put their method information in the HTTP method, instead they store it in the entity-body and in a HTTP header. </li></ul>Method Information(Contd.)
  17. 17. <ul><li>The second question is – how the client tells the server which part of the data set to operate on i.e., given that the server understands that the client wants to delete some data, how can it know which data the client wants to delete? Why should the server operate on this data instead of that data? </li></ul><ul><li>This information can be called as the Scoping Information . One obvious place to put it is in the URI path. That's what most web sites do. </li></ul><ul><li>For an example, the URI tells the server that the client wants to get a list of search results about REST. Here the method information is GET and the scoping information is /search?q=REST. </li></ul>The Second Question: Scoping Information
  18. 18. <ul><li>Many web services put scoping information in the path. In a service where the method information defines a method in the programming language sense, the scoping information can be seen as a set of arguments to that method. </li></ul><ul><li>The alternative is to put the scoping information into the entity-body. A typical SOAP service does it this way. </li></ul><ul><li>Generally, the service design determines what information is method information and what information is scoping information. </li></ul>Scoping information (Contd.)
  19. 19. <ul><li>Now that the two big questions that web services answer differently have been identified, we can group web services’s architecture into three different categories - </li></ul><ul><ul><li>RESTful resource-oriented </li></ul></ul><ul><ul><li>RPC-style </li></ul></ul><ul><ul><li>REST-RPC hybrid </li></ul></ul>Types of web service architecture
  20. 20. <ul><li>A web service can be considered as RESTful if it follows the following constraints - </li></ul><ul><ul><li>Client-server : Clients are separated from servers by a uniform interface. </li></ul></ul><ul><ul><li>Stateless : No client context being stored on the server between requests. </li></ul></ul><ul><ul><li>Cacheable : Clients are able to cache responses. </li></ul></ul><ul><ul><li>Layered system : A client cannot ordinarily tell whether it is connected directly to the end server, or to an intermediary along the way. </li></ul></ul><ul><ul><li>Uniform Interface : The uniform interface between clients and servers simplifies and decouples the architecture, which enables each part to evolve independently. </li></ul></ul><ul><ul><li>Code on demand (optional) : Servers are able to temporarily extend or customize the funtionality of a client by transferring logic to it that it can execute. </li></ul></ul>Type 1: RESTful resource-oriented web services
  21. 21. RESTful resource-oriented web services(Contd.) <ul><li>In RESTful architectures, the method information goes into the HTTP method. In Resource-Oriented Architectures, the scoping information goes into the URI. </li></ul><ul><li>This combination is really powerful because given the first line of an HTTP request to a resource-oriented RESTful web service, one can easily understand basically what the client wants to do. </li></ul><ul><li>If the HTTP method doesn't match the method information, the service isn't RESTful. If the scoping information isn't in the URI, the service isn't resource-oriented. These aren't the only requirements, but they're good rules of thumb. </li></ul>
  22. 22. RESTful resource-oriented web services(Contd.) <ul><li>A few well-known exaples of RESTful, resource-oriented web services include: </li></ul><ul><ul><li>Services that expose the Atom Publishing Protocol </li></ul></ul><ul><ul><li>Amazon's Simple Storage Service(S3) </li></ul></ul><ul><ul><li>Most of yahoo's web services </li></ul></ul><ul><ul><li>Static web sites </li></ul></ul><ul><ul><li>Many web applications, especially read-only ones like search engines </li></ul></ul>
  23. 23. Type 2: RPC-Style Architectures <ul><li>An RPC-style web service accepts an envelope full of data from its client, and sends a similar envelope back. The method and the scoping information are kept inside the envelope, or on stickers applied to the envelope. </li></ul><ul><li>HTTP is a popular envelope format, and so is SOAP. Transmitting a SOAP document over HTTP puts the SOAP envelope inside an HTTP envelope. </li></ul><ul><li>In this architecture, every object doesn't necessarily respond to the same basic interface. </li></ul><ul><li>XML-RPC protocol for the web services is the most obvious example of the RPC architecture. In this protocol, the method data and the scoping data are put inside an XML document. This XML document becomes the entity-body inside the HTTP envelope. </li></ul>
  24. 24. XML-RPC <ul><li>In XML-RPC, the XML document containing method and scoping information is put into an envelope for transfer to the server. The envelope is an HTTP request with a method, URI and headers. </li></ul><ul><li>The XML document changes depending on which method someone is calling, but the HTTP envelope is always the same. </li></ul><ul><li>Where a RESTful service would expose different URIs for different values of the scoping information, an RPC-style service typically exposes a URI for each Document Processor : something that can open the envelopes and transform them into software commands. </li></ul>
  25. 25. Type 3: REST-RPC Hybrid Architectures <ul><li>This is a term used for describing web services that fit somewhere in between the RESTful web services and the purely RPC-style services. </li></ul><ul><li>Let us consider the following URI - </li></ul><ul><ul><li> </li></ul></ul><ul><li>Despite the ”rest” in the URI, this was clearly designed as an RPC-style service, one that uses HTTP as its envelope format. It's got the scoping information in the URI, just like RESTful resource-oriented services, but the method information also goes in the URI. It gives the illusion of behaving like a RESTful web service, but it isn't. </li></ul>
  26. 26. RESTful Design Rules <ul><li>The REST architectural style describes the following six constraints applied to the architecture, while leaving the implementation of the individual components free to design: </li></ul><ul><ul><li>Client–server </li></ul></ul><ul><ul><li>Stateless </li></ul></ul><ul><ul><li>Cacheable </li></ul></ul><ul><ul><li>Layered System </li></ul></ul><ul><ul><li>Code on Demand (optional) </li></ul></ul><ul><ul><li>Uniform Interface </li></ul></ul>
  27. 27. Rule 1: Client-server <ul><li>Clients are separated from servers by a uniform interface. </li></ul><ul><li>This separation of concerns means that, for example, clients are not concerned with data storage, which remains internal to each server, so that the portability of client code is improved. </li></ul><ul><li>Servers are not concerned with the user interface or user state, so that servers can be simpler and more scalable. </li></ul><ul><li>Servers and clients may also be replaced and developed independently, as long as the interface is not altered. </li></ul>
  28. 28. Rule 2: Stateless <ul><li>The client–server communication is further constrained by no client context being stored on the server between requests. </li></ul><ul><li>Each request from any client contains all of the information necessary to service the request, and any session state is held in the client. </li></ul><ul><li>The server can be stateful; this constraint merely requires that server-side state be addressable by URL as a resource. </li></ul><ul><li>This not only makes servers more visible for monitoring, but also makes them more reliable in the face of partial network failures as well as further enhancing their scalability. </li></ul>
  29. 29. Rule 3: Cacheable <ul><li>As on the World Wide Web, clients are able to cache responses. </li></ul><ul><li>Responses must therefore, implicitly or explicitly, define themselves as cacheable, or not, to prevent clients reusing stale or inappropriate data in response to further requests. </li></ul><ul><li>Well-managed caching partially or completely eliminates some client–server interactions, further improving scalability and performance. </li></ul>
  30. 30. Rule 4: Layered system <ul><li>A client cannot ordinarily tell whether it is connected directly to the end server, or to an intermediary along the way. </li></ul><ul><li>Intermediary servers may improve system scalability by enabling load balancing and by providing shared caches. They may also enforce security policies. </li></ul>
  31. 31. Rule 5: Code on demand (optional) <ul><li>Servers are able to temporarily extend or customize the functionality of a client by transferring logic to it that it can execute. </li></ul><ul><li>Examples of this may include compiled components such as Java applets and client-side scripts such as JavaScript. </li></ul>
  32. 32. Rule 6: Uniform interface <ul><li>The uniform interface between clients and servers, discussed below, simplifies and decouples the architecture, which enables each part to evolve independently. The four guiding principles of this interface are detailed below - </li></ul><ul><ul><li>Identification of resources </li></ul></ul><ul><ul><li>Manipulation of resources through representations </li></ul></ul><ul><ul><li>Self-descriptive messages </li></ul></ul><ul><ul><li>Hypermedia as the engine of application state </li></ul></ul>
  33. 33. Uniform interface Guideline 1: Identification of resources <ul><li>Individual resources are identified in requests, for example using URIs in web-based REST systems. </li></ul><ul><li>The resources themselves are conceptually separate from the representations that are returned to the client. </li></ul><ul><li>For example, the server does not send its database, but rather, perhaps, send some HTML, XML or JSON that represents some database records expressed, for instance, in Bengali and encoded in UTF-8, depending on the details of the request and the server implementation. </li></ul>
  34. 34. Uniform interface Guideline 2: Manipulation of resources through these representations <ul><li>When a client holds a representation of a resource, including any metadata attached, it has enough information to modify or delete the resource on the server, provided it has permission to do so. </li></ul><ul><li>Clients can modify the resource using a POST request, and to delete it, it can send a DELETE request. </li></ul>
  35. 35. Uniform interface Guideline 3: Self-descriptive messages <ul><li>Each message from the server includes enough information to describe how the message can be processed by the client. </li></ul><ul><li>For example, which parser to invoke may be specified by an Internet media type (previously known as a MIME type). </li></ul><ul><li>Responses also explicitly indicate their cacheability. </li></ul>
  36. 36. Uniform interface Guideline 4: Hypermedia as the engine of application state <ul><li>Clients make state transitions only through actions that are dynamically identified within hypermedia by the server (e.g. by hyperlinks within hypertext). </li></ul><ul><li>But for simple fixed entry points to the application, a client does not assume that any particular actions will be available for any particular resources beyond those described in representations previously received from the server. </li></ul>
  37. 37. Acknowledgements <ul><li>RESTful Web Services </li></ul><ul><li>Wikipedia </li></ul><ul><li>Flickr </li></ul><ul><li>Google </li></ul><ul><li>IETF </li></ul>
  38. 38. <ul><li>Thank You </li></ul>