If the title of this post made you to think, well that’s a good sign for the remainder of this article. You might be wondering – is their really a principles of REST. To break the suspense, no i am not talking about laying back and enjoying your free time kind of rest. But i am talking about Representation State Transfer.
It has been a lot of REST talks in the web technology arena lately. Microsoft has been pushing things on their stack to embrace REST as much as possible. Evident of this fact is the release of WCF Futures known as “WCF Web API” in the MIX 11 which got over in Las Vegas just a week ago.
I went back to some reading on this area and wanted to know what WCF had in terms of this aspect of REST. I specifically looked in WCF 3.5 as it was in this version that we had for the first time the support for RESTful services using WCF. In the journey to find the right information i landed on a MSDN article by Justin Smith titled – “HTTP Programming with WCF and .NET Framework 3.5”. Do go over this article. I am going to express my view of a very particular topic that the author has made in the article.
What struck me as i read through this article was the simplicity in which the author gives you information of REST and what you should take care to join the bandwagon of RESTful services on the WEB. The author talks about important principles to take care when moving to a RESTful architecture for your applications. And here i quote:
Embrace the URI
HTTP GET is Special
Content-Type is the data model
If you have had any reading or understanding of the topic of REST, you will nod with me when i say – this is the layman way of explaining how to become RESTful.
Embracing the URI – is nothing but the fact of designing your resources which can be addressable by a specific address. for e.g. if you services exposes Artists data you can have the following URLs defined:
– this will return all artists in the system
– this will return a particular artist with name “Bryan Adams”.
So first thing to work out if you want to be RESTful service are these URL addressing for your resources.
HTTP GET is Special – is nothing but the fact that instead of your service supporting operations like GetArtist(), GetAlbum(), GetSchedule(), etc. etc. now it will be just a plain old HTTP request for returning Artists, Albums, Schedule or whatever your application supports. So its all the HTTP GET in the rest world for reading the data.
For e.g. To read a list of Albums, the URI you support might be – //Albums">http://<server>/<service>/Albums. When a client makes this request the HTTP messaging will look like below:
GET /myservice/Albums HTTP/1.1
HOST: www.yoursite.comAs you can see in REST world the read of a resource is always a GET.
Content-Type is the data model – is nothing but the fact that the payload that comes through the HTTP is nothing but the data that is getting exchanged over the wire. Content-Type will define the type of the data being exchanged. Content-Type is the token between the client and the server to know what the client is capable of handling and what the server should return back. This will give the server the ability to modify the data payload being sent back to the client. For e.g. if the Content-Type is text/xml – then the server can use the Xml Serializer to shape the data or if the Content-Type is text/json – then the server can use JsonSerializer to shape the data.
So far, this article by Justin Smith really made me look at the definition of REST in a much more simplistic perspective. Now i am ready to embrace REST and start to dig deeper in to the RESTful world with WCF. My aim with this article was to throw some light on REST from a layman’s perspective. Stay tuned for a series of posts on this subject.
As usual, till next time, Happy Coding. Code with passion, Decode with patience.