REST API and RESTful Service

Hello friends,

I've been working on a project that uses REST API and I found it interesting. Just thought of sharing with you all :)
As you might have probably know, A service that is created upon the architecture of REST is called as Rest service. Although REST looks more inclined to web and HTTP its principles can be applied to other distributed communication systems as well. I found that World Wide Web(WWW) is one of the real implementation of REST architecture. RESTful services or REST based services are easy to create and cab be consumed from variety of devices.
Little bit more about REST, REST stands for "Representational State Transfer" and that represents a set of principles for creating distributed applications in web. Unlike the traditional RPC-style SOAP services which are using HTTP just as a transport layer, REST uses all the advantages of the HTTP. For example request verbs, URI, media-types, caching, security and all. Since REST services works like a normal website they are easy to create and consume compared to the RPC-style web services.

Let me focus your attention on some of the HTTP request methods and how to use them in REST services.

Common Request Methods 

Popular: GET, POST
Others : PUT, DELETE, HEAD, OPTION

Whenever a browser is sending a request it uses one of the above request methods. As i have mentioned there are several number of different request methods. Some may look identical at a glance but functionality wise, they have their own differences. However the most popular HTTP methods are GET and POST as all of us may have already used in our web applications. When you type a website url in the browser address bar, for example
http://www.example.com
actually you are issuing a GET request to the server and when you are making a payment through a payment gateway, all your sensitive data are issued using a POST request.

Let me focus on GET and POST.

GET  : 
The GET method is used for getting data from the server. The data may be anything, HTML document, an Image, XML file etc...
Ex for GET request :  http://www.example.com/getUser?id =14

Here we should be careful to use GET requests only to fetch some information from the server. In otherwords, the request should not change the database or file or anything in the server. Now in a REST service, you should always use GET request to only return data. Not for adding, modifying or deleting.

POST :

The POST method is the powerful method in the HTTP protocol. It can be used for any operation and it is totally depend upon the server. A POST request can be used to create a new resource or to update an existing resource in the server. Most of the timest the POST request is confused with the PUT request in terms of which CRUD (Create, Read, Update, Delete) operations it should perform on the server. Actually both POST and PUT can be used in  create as well as in update operations but the difference comes in idempotency. If a request is said to be idempotent then how many times that request is issued repeatedly to the server, you get the same result as in the first request. PUT is idempotent but POST is not. For example when you are creating new users everytime the server state is different. On the other hand an Idempotent PUT request do the same first different in all the serial requests. For ex, changing the last name of a particular user.

In the REST architecture the POST is more linked with the Create operation. A post request would look like this,
(Following shows a jQuery Ajax request)
  
==========================================================
$.ajax({
             
 type: "POST",
 url: "/User/Insert",               
data: { id: 17 , fname: Tom, lname: Cruise, age: 50  },
success: function (result) {
if (result.Status == "SUCCESS") {
alert("Operation Successful");

}});

==========================================================

Next time, I will show you a simple implementation of REST API using ASP.net MVC 4.

Regards,

Comments

Popular posts from this blog

Dependency Injection in ASP.NET MVC 5 with StructureMap

Real-time web with Node.js & Socket.io

First Look at AngularJS 2.0