REST is short for ‘Representational State Transfer’. REST is resource-based rather than action based like some other service systems. In a SOAP based system you would typically call a method that performs an action like GetUser. In a REST service you would simply identify the resource you needed, a User, and the HTTP verb describes the action to be taken with that resource. These resources are identified by URI’s. The resource exists on the sever and a representation of that resource is sent to the client, often in JSON format. The representation can be all or part of the resource and may include additional meta data about the resource. So you may have multiple representations of a resource. Restful architecture is based on six constraints. While there are different technologies that can be used to meet these constraints I will use the more common technologies to describe each of the constraints.
Here are the Six Constraints:
- Uniform interface
- Layered system
- Code on demand
Uniform Interface: A defined and standardized interface between the client and server. This simplifies and decouples the interface. Typically HTTP fills this roll. The HTTP verbs are used to define what action is to be taken, URI’s are used to identify the resource that is being worked with, and the HTTP response, both status and body, are used for sending the results to the client. Since HTTP is so ubiquitous it provides a very uniform interface that is accessible from almost any type of client.
Stateless: Every request to the server should be stateless, meaning all of the information required to carry out the request should be included in the request itself. There server should not have to retain or know anything about the client or any previous requests in order to complete the current request. Any state information will be the client’s responsibility.
Client-server: The relationship between the client and server should be disconnected, the client should not rely on a persistent connection to the server. This provides a separation of concerns with the client focused on the user interface and the server providing services.
Cacheable: The representations that are sent back to the client should be cacheable. There are three different flavors of cacheable representations. First is implicitly, if no other information is returned about the caching of the representation then it is implicitly cacheable. It could be explicitly cacheable meaning the server will return information about the age or duration of the cacheable representation. The third is a negotiated cacheable representation between the client and the server.
Layered System: A layered system should be assumed by the client. Meaning the client should not assume a direct connection to the server. There could be a number of layers, both hardware and software between the client and server. The client could receive a cached response from one of these layers and in fact may not reach the server at all. This provides for a very scalable system.
By following these constraints restful services are scalable, simple, easily modified, visible, portable, and reliable.