Self Learning : REST concept–Part 1

It seems a long time that I read concepts behind HTTP, XML and SOAP and today there are new terms REST and JSON. I started learning concepts behind these new terms and writing my learning’s for my self reference

A traditional web service has become something of  an interface that exposes methods. Clients that are aware of the method’s name, input and output parameters invoke the methods. Let consider some business scenarios

  • A calculator service offering a method Add() that takes two integers as input parameters and returns the output.
  • A stock exchange service offering a method GetStockPrice() that takes stock symbols as input and returns the latest stock price.
  • A banker service offering a method Transfer() that takes two account numbers and a specific money.

Let us start thinking what happens when one specifies a URL on the browser. The browser gets information about the object from a remote server. How does it perform this?If we consider URL as "the object", the browser GET information "on that object" from a remote server. if we consider HTTP as object, then we have methods GET,PUT,POST(and other HTTP verbs) . If everything on the web is performed by one of the HTTP verbs, why should our web service expose more than 4 operations? Rather why not we expose only objects that support these four operations and any client should be able to perform anything with that web service.

A REST web service is one that exposes only objects, and supports a set of verbs on those objects. So returning to our stock exchange example, your web service that exposes stock symbols that will also support GET operation on the stock symbols. Hence when a client sends a request that reads like “GET TCS”, the response could be TCS stock price or dividend or CEO name. But you write your web service and the web service would only perform sending the latest stock price and not other information,

The data that comes back can constitute the "HTTP Body (or content)". This can be in XML format or HTML format or plain format and the "Content-type" header specifies it. Are we talking of Web application?

  • The difference between web application and a web service is that the HTTP body or content in response to GET significantly varies in content and format.
  • A browser hardly needs to issue any command other than GET and most of the we applications are not coded to react for PUT or DELTE, but most are coded for POST as POST is (misused) used for any generic request that brings some incoming data and takes back some outgoing data.

Effectively a web service differs only in usage from a web site and the request/ response protocol is same. Why do we call it a web service? Why should classes and interfaces have methods like GetStockPrice()?

Should we go ahead and replace the traditional web method calls with the simple HTTP actions to achieve everything? this is not going to be easy. To replace all methods with a few simple verbs needs a lot of effort and motivation and one needs to start looking at stateless architecture to reach a  web like request/ response mechanism

  • Let us assume that Transfer method needs to implemented as part of REST web service. Think aloud how will you do that. You cannot design a method like GetTransfer. Let me start thinking. I should get out of my desktop programming style.
  • Let us take statelessness. Any call should not refer to any previous call and calls are independent. The server should not know what happened during a previous call while processing a previous call.Let us think of login(). it is state aware call as the web service remembers that the user has already logged in a previous call.