Representational State Transfer (REST)Representational State Transfer (REST) is a style of softwarearchitecture for distributed systems such as the World Wide Web. REST has emerged as apredominant Web service design model.Key goalsKey goals of REST include: Scalability of component interactions Generality of interfaces Independent deployment of components Intermediary components to reduce latency, enforce security and encapsulate legacy systemsREST has been applied to describe the desired web architecture, to help identify existingproblems, to compare alternative solutions, and to ensure that protocol extensions would notviolate the core constraints that make the Web successful.ConstraintsThe REST architectural style describes the following six constraints applied to the architecture,while leaving the implementation of the individual components free to design:Client–server A uniform interface separates clients from servers. 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. Servers are not concerned with the user interface or user state, so that servers can be simpler and more scalable. Servers and clients may also be replaced and developed independently, as long as the interface between them is not altered.Stateless The client–server communication is further constrained by no client context being stored on the server between requests. Each request from any client contains all of the information necessary to service the request, and any session state is held in the client.Cacheable As on the World Wide Web, clients can cache responses. 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. Well-managed caching
RESTful web servicesA RESTful web service (also called a RESTful web API) is a web service implemented using HTTPand the principles of REST. It is a collection of resources, with four defined aspects: the base URIfor the web service, such as http://example.com/resources/ The Internet media type of the data supported by the web service. This is often XML but can be any other valid Internet media type providing that it is a valid hypertext standard. The set of operations supported by the web service using HTTP methods (e.g., GET, PUT, POST, or DELETE). The API must be hypertext driven. The following table shows how the HTTP methods are typically used to implement a web service. RESTful Web Service HTTP methods Resource GET PUT POST DELETE Create a new entry in the collection. The List the URIs and Replace the new entrys Collection URI, such perhaps other Delete entire collection URL isashttp://example.com/re details of the the entire with another assigned sources/ collections collection. collection. automatically members. and is usually returned by the operation. Not generally Retrieve a used. Treat representation of Replace the Delete the addressed the addressed addressed the Element URI, such member as a member of the member of the addressedashttp://example.com/re collection in its collection, collection, or if member sources/item17 own right expressed in an it doesnt exist, of the and create a appropriate Internet create it. collection. new entry in media type. it.
The PUT and DELETE methods are idempotent methods. The GET method is a safe method (ornullipotent), meaning that calling it produces no side-effects.Unlike SOAP-based web services, there is no "official" standard for RESTful web services. This isbecause REST is an architectural style, unlike SOAP, which is a protocol. Even though REST is nota standard, a RESTful implementation such as the Web can use standards like HTTP, URI, XML,etc.Why is it called Representational State Transfer?The Web is comprised of resources. A resource is any item of interest. For example, theBoeing Aircraft Corp may define a 747 resource. Clients may access that resource with thisURL:http://www.boeing.com/aircraft/747A representation of the resource is returned (e.g., Boeing747.html). The representationplaces the client application in a state. The result of the client traversing a hyperlink inBoeing747.html is another resource is accessed. The new representation places the clientapplication into yet another state. Thus, the client application changes (transfers) statewith each resource representation --> Representational State Transfer!Here is Roy Fieldings explanation of the meaning of Representational State Transfer:"Representational State Transfer is intended to evoke an image of how a well-designed Webapplication behaves: a network of web pages (a virtual state-machine), where the userprogresses through an application by selecting links (state transitions), resulting in the nextpage (representing the next state of the application) being transferred to the user andrendered for their use."REST - An Architectural Style, Not a StandardREST is not a standard. You will not see the W3C putting out a REST specification. You willnot see IBM or Microsoft or Sun selling a REST developers toolkit. Why? Because REST isjust an architectural style. You cant bottle up that style. You can only understand it, anddesign your Web services in that style. (Analogous to the client-server architectural style.There is no client-server standard.)While REST is not a standard, it does use standards: HTTP URL XML/HTML/GIF/JPEG/etc (Resource Representations) text/xml, text/html, image/gif, image/jpeg, etc (MIME Types)
REST Web Services CharacteristicsHere are the characteristics of REST: Client-Server: a pull-based interaction style: consuming components pull representations. Stateless: each request from client to server must contain all the information necessary to understand the request, and cannot take advantage of any stored context on the server. Cache: to improve network efficiency responses must be capable of being labeled as cacheable or non-cacheable. Uniform interface: all resources are accessed with a generic interface (e.g., HTTP GET, POST, PUT, DELETE). Named resources - the system is comprised of resources which are named using a URL. Interconnected resource representations - the representations of the resources are interconnected using URLs, thereby enabling a client to progress from one state to another. Layered components - intermediaries, such as proxy servers, cache servers, gateways, etc, can be inserted between clients and resources to support performance, security, etc.Principles of REST Web Service Design1. The key to creating Web Services in a REST network (i.e., the Web) is to identify all ofthe conceptual entities that you wish to expose as services. Above we saw some examplesof resources: parts list, detailed part data, purchase order.2. Create a URL to each resource. The resources should be nouns, not verbs. For example,do not use this:http://www.parts-depot.com/parts/getPart?id=00345Note the verb, getPart. Instead, use a noun:http://www.parts-depot.com/parts/003453. Categorize your resources according to whether clients can just receive a representationof the resource, or whether clients can modify (add to) the resource. For the former, makethose resources accessible using an HTTP GET. For the later, make those resourcesaccessible using HTTP POST, PUT, and/or DELETE.4. All resources accessible via HTTP GET should be side-effect free. That is, the resourceshould just return a representation of the resource. Invoking the resource should not resultin modifying the resource.5. No man/woman is an island. Likewise, no representation should be an island. In otherwords, put hyperlinks within resource representations to enable clients to drill down formore information, and/or to obtain related information.6. Design to reveal data gradually. Dont reveal everything in a single response document.Provide hyperlinks to obtain more details.
7. Specify the format of response data using a schema (DTD, W3C Schema, RelaxNG, orSchematron). For those services that require a POST or PUT to it, also provide a schema tospecify the format of the response.8. Describe how your services are to be invoked using either a WSDL document, or simplyan HTML document.SummaryThis article described REST as an architectural style. In fact, its the architectural style ofthe Web. REST describes what makes the Web work well. Adhering to the REST principleswill make your services work well in the context of the Web.References 1. http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm 2. http://www.wikipedia.org/