RESTful Web Services — An Introduction
REST (Representational State Transfer) is an architectural style introduced by Roy Thomas Fielding which is used to develop and consume web services. RESTful web services are designed to use a stateless communication protocol - HTTP (Hypertext Transfer Protocol).
Using only four HTTP request methods -GET, POST, PUT, DELETE, we can fulfill all our business demands while accessing a resource.
Let’s have a look so as to understand when to use what —
1. GET — Used to fetch data from the server. It is said to be idempotent, i.e. gives us the same set of response no matter how many times we send the same request.
2. POST — Used to store data on the server side which is sent from the client side. This method is non-idempotent.
3. PUT — Used to modify or replace an existing data on the server side. This method is idempotent.
4. DELETE — Used to delete the specified resource. Like the PUT method, DELETE is also idempotent.
Any RESTful web service should be stateless, i.e. it should not keep any client state on the server side. This restriction is called statelessness. Every client request must contain all of the necessary details required for the server to understand the request. Thus every HTTP request happens in complete isolation. Interactions of the previous requests are not stored and must not affect the future requests. Definitely this costs us the overhead of sending the entire data again and again which is needed for the server to understand how to serve the request.
Let’s look at some of the advantages of using a stateless architecture —
1. Scalability — Once a request has been served the server can quickly free the resources which were required to serve the request. This reduces the extra overhead of managing resource usage over requests. Thus we can have any server serving the request because we don’t have any session related dependency.
2. Visibility — A client side request must contain all the necessary details required for the server to understand fully what the client wants. This takes care of the fact that a request has no extra dependencies. The server does not need to find anything else other than the request in order to understand the full nature of the request.
3. Reliability — As every request exists in complete isolation, even if any other requests fail, it should not affect other requests.
The term representational in REST means that the transfer of data between client and server must be in some representational (XML, JSON, etc.) format. Generally, performance wise JSON is considered as a lightweight alternative to XML.
Once the client requests for some information from the server, the current state of the resource is sent back as response. Next time a request is made the state of the resource could be different and thus result in a different response. This is what results in state transfer.
In a nutshell, web services which support data in some representational format (XML, JSON, etc.) and using HTTP protocol the current state of the data can be transferred, are said to be RESTful web services.