What do we mean by RESTful?

The word REST is becoming increasingly popular these days. Most of us do have our set of understanding of it, but does that give you a wholesome picture? Let's try understanding it. 

REST stands for Representational State Transfer. What does that mean?..
  • An architectural style which talks about communication between client (requesting a resource) and server (holding a resource OR a handle of resource), with the help of a generic interface. 
  • It's a "representational state transfer" because all communications (transfer) takes place via a set of representations which tell clearly about the current or expected state on server. 
  • It's stateless, that means server doesn't have to maintain state (you can say sessions also) for every the client it is interacting with. Each request in itself is sufficient to serve it.
Lets try understanding it more clearly from the HTTP and URI concept.
  • HTTP becomes the interface
  • HTTP + URI is a representation which is transferred between Client and Server: Consider a server holding information about the "Products" which Company X is working with. The company wants to expose a service which it's clients can use to work on products. i.e. "create", "modify", "fetch" or "delete". They can easily make use of URI's and HTTP verbs to implement their requirement.
          Request A:
          URI - ../Products OR ../Products/{id}
          Verb - GET
          Requirement met: fetching multiple/single records.

          Request B: 
          URI - ../Products?desc=ProductX
          Verb - POST
          Requirement met - ProductX is created.

          Request C:

          URI - ../Products?id=1101&desc=ProductY
          Verb - PUT
          Requirement met - the description of product with id 1101 is changed to "ProductY"

          Request D:
          URI - ../Products?id=1101
          Verb - DELETE
          Requirement met - the product with id 1101 is deleted.

Notice that each request is sufficient to provide all information required to serve it.
e.g "Request D" clearly tells that the operation going to happen is a "DELETE"; it will occur on Products collection and will delete a product  with id 1101.

As said earlier REST is simply an architecture style and is not limited to HTTP. HTTP is just an implementation of RESTful principle where HTTP verbs and URI help in it's implementation.

RESTful services or ASP.net MVC4 Web API is build on top of this concept.

Comments

Popular Posts