Representational state transfer
Representational state transfer is a software architectural style that defines a set of constraints to be used for creating Web services. Web services that conform to the REST architectural style, called RESTful Web services, provide interoperability between computer systems on the internet. RESTful Web services allow the requesting systems to access and manipulate textual representations of Web resources by using a uniform and predefined set of stateless operations. Other kinds of Web services, such as SOAP Web services, expose their own arbitrary sets of operations.
"Web resources" were first defined on the World Wide Web as documents or files identified by their URLs. However, today they have a much more generic and abstract definition that encompasses every thing, entity, or action that can be identified, named, addressed, handled, or performed, in any way whatsoever, on the Web. In a RESTful Web service, requests made to a resource's URI will elicit a response with a payload formatted in HTML, XML, JSON, or some other format. The response can confirm that some alteration has been made to the resource state, and the response can provide hypertext links to other related resources. When HTTP is used, as is most common, the operations available are GET, HEAD, POST, PUT, PATCH, DELETE, CONNECT, OPTIONS and TRACE.
By using a stateless protocol and standard operations, RESTful systems aim for fast performance, reliability, and the ability to grow by reusing components that can be managed and updated without affecting the system as a whole, even while it is running.
The term representational state transfer was introduced and defined in 2000 by Roy Fielding in his doctoral dissertation. Fielding's dissertation explained the REST principles that were known as the "HTTP object model" beginning in 1994, and were used in designing the HTTP 1.1 and Uniform Resource Identifiers standards. The term is intended to evoke an image of how a well-designed Web application behaves: it is a network of Web resources where the user progresses through the application by selecting resource identifiers such as
History
defined REST in his 2000 PhD dissertation "Architectural Styles and the Design of Network-based Software Architectures" at UC Irvine. He developed the REST architectural style in parallel with HTTP 1.1 of 1996–1999, based on the existing design of HTTP 1.0 of 1996.In a retrospective look at the development of REST, Fielding said:
Architectural properties
The constraints of the REST architectural style affect the following architectural properties:- performance in component interactions, which can be the dominant factor in user-perceived performance and network efficiency;
- scalability allowing the support of large numbers of components and interactions among components. Roy Fielding describes REST's effect on scalability as follows:
- simplicity of a uniform interface;
- modifiability of components to meet changing needs ;
- visibility of communication between components by service agents;
- portability of components by moving program code with the data;
- reliability in the resistance to failure at the system level in the presence of failures within components, connectors, or data.
Architectural constraints
The formal REST constraints are as follows:
Client-server architecture
The principle behind the client-server constraints is the separation of concerns. Separating the user interface concerns from the data storage concerns improves the portability of the user interfaces across multiple platforms. It also improves scalability by simplifying the server components. Perhaps most significant to the Web is that the separation allows the components to evolve independently, thus supporting the Internet-scale requirement of multiple organizational domains.Statelessness
The client-server communication is constrained by no client context being stored on the server between requests. Each request from any client contains all the information necessary to service the request, and the session state is held in the client. The session state can be transferred by the server to another service such as a database to maintain a persistent state for a period and allow authentication. The client begins sending requests when it is ready to make the transition to a new state. While one or more requests are outstanding, the client is considered to be in transition. The representation of each application state contains links that can be used the next time the client chooses to initiate a new state-transition.Cacheability
As on the World Wide Web, clients and intermediaries can cache responses. Responses must, implicitly or explicitly, define themselves as either cacheable or non-cacheable to prevent clients from providing stale or inappropriate data in response to further requests. Well-managed caching partially or completely eliminates some client-server interactions, further improving scalability and performance.Layered system
A client cannot ordinarily tell whether it is connected directly to the end server, or to an intermediary along the way. If a proxy or load balancer is placed between the client and server, it won't affect their communications and there won't be a need to update the client or server code. Intermediary servers can improve system scalability by enabling load balancing and by providing shared caches. Also, security can be added as a layer on top of the web services, and then clearly separate business logic from security logic. Adding security as a separate layer enforces security policies. Finally, it also means that a server can call multiple other servers to generate a response to the client.Code on demand (optional)
Servers can temporarily extend or customize the functionality of a client by transferring executable code: for example, compiled components such as Java applets, or client-side scripts such as JavaScript.Uniform interface
The uniform interface constraint is fundamental to the design of any RESTful system. It simplifies and decouples the architecture, which enables each part to evolve independently. The four constraints for this uniform interface are:; Resource identification in requests
; Resource manipulation through representations
; Self-descriptive messages
; Hypermedia as the engine of application state
Applied to Web services
Web service APIs that adhere to the [|REST architectural constraints] are called RESTful APIs. HTTP-based RESTful APIs are defined with the following aspects:- a base URI, such as
;http://api.example.com/collection/ - standard HTTP methods ;
- a media type that defines state transition data elements. The current representation tells the client how to compose requests for transitions to all the next available application states. This could be as simple as a URI or as complex as a Java applet.
Relationship between URI and HTTP methods
HTTP METHODS | Resources that manipulate data, such as or
| Resources that invoke operations, such as
|
POST | Invoke the operation provided by the resource. If the request results in the creation of a new resource, its URI is returned in the response Location header. | |
GET | Retrieve a representation of the data in the response body. | Retrieve the status of an asynchronous operation in the response body. |
PUT | Store the representation in the request body as the state of the resource. Subsequent GET access on the resource is expected to return this representation with HTTP status 200 until PATCHed, a different representation is PUT, or the state of the resource is DELETEd. | |
PATCH | Update some part of the resource's state using the instructions in the request body. | |
DELETE | Delete the state of the resource. Subsequent GET access on the resource is expected to return HTTP status 404 until new state is PUT to it. | Cancel an asynchronous operation. |
The GET method is safe, meaning that applying it to a resource does not result in a state change of the resource. The GET, PUT and DELETE methods are idempotent, meaning that applying them multiple times to a resource results in the same state change of the resource as applying them once, though the response might differ.
Discussion
Unlike SOAP-based Web services, there is no "official" standard for RESTful Web APIs. This is because REST is an architectural style, while SOAP is a protocol. REST is not a standard in itself, but RESTful implementations make use of standards, such as HTTP, URI, JSON, and XML. Many developers also describe their APIs as being RESTful, even though these APIs actually don't fulfill all of the architectural constraints described above.The following table shows how HTTP methods are typically used in REST APIs based on HTTP with historical method semantics.
HTTP METHODS | Collection resource, such as
| Member resource, such as
|
GET | Retrieve the URIs of the member resources of the collection resource in the response body. | Retrieve representation of the member resource in the response body. |
POST | Create a member resource in the collection resource using the instructions in the request body. The URI of the created member resource is automatically assigned and returned in the response Location header field. | Create a member resource in the member resource using the instructions in the request body. The URI of the created member resource is automatically assigned and returned in the response Location header field. |
PUT | Replace all the representations of the member resources of the collection resource with the representation in the request body, or create the collection resource if it does not exist. | Replace all the representations of the member resource or create the member resource if it does not exist, with the representation in the request body. |
PATCH | Update all the representations of the member resources of the collection resource using the instructions in the request body, or may create the collection resource if it does not exist. | Update all the representations of the member resource, or may create the member resource if it does not exist, using the instructions in the request body. |
DELETE | Delete all the representations of the member resources of the collection resource. | Delete all the representations of the member resource. |