Thursday, May 26, 2011

CREATE RESTful WCF Service API Using POST : Step By Step Guide

Introduction
In my previous article I tried to explain about WCF Restful service using HTTP Get method.  This works well as long as you are sending small data as information to the service. But if want to deliver huge data, HTTP GET Method is not a good choice.  In this article we will create one WCF RESTFul POST API.  I will also try to explain why and where to use POST method. 

Why to use POST, not GET?

When i write any WCF service, i always use POST. Reason is the advantage of POST over GET. Using HTTP POST method, you can almost achieve everything which you can achieve from GET. Despite of this you will get some additional feature if use POST. We might say that "GET" is basically for just getting (retrieving) data whereas "POST" may involve anything, like storing or updating data, or ordering a product, or sending E-mail etc.

POST
1) Easy Character Encoding using application/x-www-form-urlencoded
2) No Proxy by default so always actual data from web server. 
3) Data length can be restricted by webserver, not by browser.

GET  
1) Character encoding will reduce the amount of data that can be used because of url encoding entities (i.e. three Japanese characters are converted to this: %26%2312454%3B%26%2312455%3B%26%2312502%3B)
2) Running a http request with GET can be cached on your web browser or a configured proxy server.
3) Maximum URL length is 2,083 characters in Internet Explorer (see MaxClientRequestBuffer: Use POST instead of GET to Send Large Amounts of Data in Request)
and lot more

Extremely long URLs are usually a mistake. URLs over 2,000 characters will not work in the most popular web browser. Sending long information via URL is not a good way of implementation and also there has many restrictions i.e. max length of URL, Information format bla bla bla. For example Internet Explorer has a limitation implemented at 2083 characters. URIs is meant to be readable not to send information.

So if you are writing any REST service and your information is long enough, better to choose POST instead of GET method.

In this article, i am going to create WCF Restful service using POST method and access it using HTTP Request. So we will have client and server both in this example code. I am creating one service which accepts HTTP Post XML request and response request data in XML format.

Step by Step Guide

STEP-1) 
Launch Visual Studio 2010. Click FILE->NEW->PROJECT. Create new "WCF Service Application". .

Read more: Codeproject