REST Fundamentals


Introduction

REST stands for Representational State Transfer, is an architectural style where you can build the software system in which clients (user agents) can make requests to the services [end points]. REST one of the ways to implement the client\server architectural style. REST can be used to build software applications in which clients can make requests of services.

You can read WCF vs Web services article to better understand this article.


 
In the above figure client interacting services using three named a, b and c requests. Each request has been routed through the resource origin server.

Request a has been sent to a local proxy, which in turn can access a caching gateway[not represented in the figure] for DNS lookup and forwards the request to an origin server whose internal resources are defined by an encapsulated object in the request itself.

Request b is sent directly to an origin server, which is able to satisfy the request from its own cache.

Request c is sent to a proxy that is capable of accessing WAIS[wide area information system] which is outside the Web architecture, and translates the WAIS response into a format recognized by the generic connector interface.

REST systems are built on following principles:

  • Clients[User Agents] interact with resources via Uniform Resource Identifier (URI).
  • Interaction with Resources is accomplished with HTTP standard protocol and with the HTTP media type(XHTML, XML,JPG,PNG etc).
  • Resources are self-descriptive. All information about resource is contained inside the request itself.
  • Resources are interconnected through the links.

Example

Imagine I needed to build a service which tells all of the years and article names that techbubbles has published.

Follow the steps to build the RESTful service,

  1. What are your resources going to be?
  2. What are the URIs that represent those resources?
  3. What parts of the HTTP verbs is each URI going to support?
  4. How you are going to manage the Resource state?

Here the resources are all of the years and articles that techbubbles has been published. You can use any media type to represent the resources. Here i am using XML mediatype.

Next, I am going to determine the URI's for each resource, we need to determine relative URI's of the service. The list of years will be the root URI of the service.

Example: /{year}/ and /{year}/Articles

Resource      URI      Verbs
All years       "/"       GET

If we want to consume the January 2009 articles as a client, we would do an HTTP GET to /2006/January.

Why should we use REST?

As a developer you probably can ask this question and why can not we use the Remote procedure call (RPC) such as Remoting or Interoperable technologies such as SOAP or WCF.

According to Microsoft there are two main reasons

  1. REST offers some significant features and benefits over RPC in many cases.
  2. Microsoft is moving many of its own implementations away from RPC technologies and toward REST.

Even if you are not convinced to use REST to build software systems, as more frameworks and technologies from Microsoft move to REST, you'll need to know how to interact with them.

REST Advantages

Caching RESTful endpoints can implement caching to improve speed and scalability.

Scale-Out REST encourages each resource to contain all of the states necessary to process a request.

Interoperability SOAP achieves the interoperability but still some languages and environments don't have SOAP toolkits. REST only requires an HTTP library to be available for most of the operations.

Simplicity REST relates resources to URI's and the uniform interface. You can type different URIs for accessing different resources which much similar to typing URLs in browser.