Rest basically means using current features of web in simple and effective way.
Features of Web :
Old and widely accepted HTTP protocol.
This HTTP protocol has 4 methods – GET, POST, PUT and DELETE
HTTP protocol is stateless.
URI- Uniform resource identifier via which we locate any service on the web.
REST takes these principles from web and applies certain principles to them and that is what is called as REST; It is nothing but architectural style built on certain principles using the current HTTP “Web” fundamentals.
There are five basic fundamentals in HTTP web which we are leveraged to create web services.
1st principle-Everything is a resource
If we talk about internet, it is all about getting data; data can be in the form of web page, an image, video file etc; it can also be a dynamic output i.e customer data, news subscription etc.The best way to see REST is seeing data as a resource, be it be a physical file, image etc ..
Example of resources with URI
http://www.GeetikaBatra.com/image/logo.gif (Image Resource)
http://www.GeetikaBatra.com/customer/100 (Dynamically pulled resource)
http://www.GeetikaBatra.com/videos/0001 (Video resource)
http://www.GeetikaBatra.com/home.html (Static resource)
Or any other data we want from internet is a resource from REST point of view.
2nd Principle-Every resource is identified by a unique identifier
It identifies all the resources from URI. for example if we want to display a customer with the orders, we can use http://www.GeetikaBatra.com/DisplayCustomerAndOrder.aspx. In REST, there is one more constraint to the URI, every URI should uniquely represent any resource state, in other words; if I want to get a details of a customer named “Shiv”; I can use http://www.GeetikaBatra.com/customer/Shiv .
Rather than pointing to the generic page like http://www.GeetikaBatra.com/DisplayCustomerAndOrder.aspx , we can actually point to a unique identifier and can retrieve a unique data.
It uses a simple and uniform interface; simplification is a way to success i.e the goal of REST is to have uniform interfaces. When external clients are interacting with my service; they expect a simplified interface. For e.g If I have a customer that orders data, which I have exposed on the web and these are some function names by which external client can communicate
The above methods seem to be inconsistent and difficult to remember, this is what REST says; keep your interfaces uniform and simple. This can be achieved by uniform method of HTTP protocol and combining the name with resource operation. In HTTP protocol, there are lots of common methods e.g creating, updating, deleting etc.
GET Gets a resource
PUT Creates and updates a resource
POST Submit data to resource
DELETE Deletes a resource
The above are traditional methods and if we can get these traditional methods and combine with our resource, then our interface can be made uniform. If we combine the HTTP methods to the REST uniform API, we can have it better, for example if a customer is to be added, then we can send HTTP PUT request to REST i.e customer/shiv.
Uniform interfaces is nothing but your old combination of HTTP’s POST, GET, PUT, DELETE with our resource names. By doing this, we can achieve a simplified communication.
All communications are done via representations, example if there is a client, a server and inside the server, there are resources; whenever the client sends some data to server and server sends back a response, everything is done via representations, so any kind of req and response which goes from the client to the server and back again is nothing but representations.
If you want to make a new customer record, what should you do?
Above is an example of representation.This can be done by HTTP PUT. Once the resource is created, you will receive something like this. Following representation is a response of above.
Formats can vary, it can be JSON or executable format etc.
All the requests and responses from client to server are called as representations.
5th Principle-Be Stateless
Every request should be an independent request, so that we can scale up using load balance etc. Independent requests means, with the data, you also send the state of the request, so that with the service, we can carry forward the same for the next level.
Above representation sends user ID and password to a system so that it can be validated. we may get this back.
Let us say that we want to go and search a customer. Now, we will send a representation and we will also say that we are currently logged in successfully and do not again ask us the user ID and password. So we will set something like flag
In other words, every request is independent i.e it is stateless.