Click here to Skip to main content
11,922,416 members (32,025 online)
Click here to Skip to main content
Add your own
alternative version


90 bookmarked

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

, 25 May 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
This article will help you to create RESTful WCF POST API. I have given Client and Server example which is really easy to understand.


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. The reason is the advantage of POST over GET. Using HTTP POST method, you can almost achieve everything which you can achieve from GET. Despite this, you will get some additional feature if using 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.


  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


  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 an 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 it 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. URI 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


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


Once you create the project, you can see in solution that by default WCF service and interface file are already created. Delete by default created file as we will create our own interface and WCF service file.



Now right click on solution and create one new WCF service file. I have given name to the service file as “RestServiceImpl.svc”.


We will create two DataContract classes for request and response purposes.


RequestData will receive the request from the client and ResponseData response. One very important thing is that POST XML format to the service should be the same as RequestData. So the post XML request format can be like below:


We must have to use namespace for the data contract in POST RESTFul service, which has multiple uses, i.e., reflects the current version of your code. The same namespace should use in XML to Post data. Here, I have given as namespace which can be changed.


Now we will write the OperationContract in IRestServiceImpl which is an interface. Below is the code for that.

As we can see in the above code, here Request and Response format is XML. We can change it to JSON also by changing the WebMessageFormat from WebMessageFormat.Xml to WebMessageFormat.Json.


Implementation of Auth OperationContract in RestServiceImpl.svc.cs here.



Now let’s move to the configuration part which is the last one. There will be two basic parts of the configurations file which we must have to understand.



And that’s it. Our Restful WCF POST RESTful service is ready to test.


I have created one Test WebClient in C# which makes HTTP request with some XML data. My application is hosted on localhost and URL is http://localhost:35798/RestServiceImpl.svc/auth. I launched and invoked service using WebClient. Below is my XML response which is the correct one.

This service can be used by any language application (.NET, PHP, JAVA, etc.) and that is the major reason to use REST service in the application.

Hope the article is useful for the community. Comments, suggestions and criticisms are all welcome.


  • 25th May, 2011: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

ashutosh k. shukla
Software Developer (Senior)
Singapore Singapore
A versatile person having 10 Years of IT Industry experience in software development on .NET and Android Technology.

You may also be interested in...

Comments and Discussions

SuggestionAbove code and Fourthbottle code helped me a lot to know WCF Pin
lawrine7-Oct-15 5:01
memberlawrine7-Oct-15 5:01 
QuestionHow to Invoke this Service? Pin
Member 1193173016-Sep-15 6:28
memberMember 1193173016-Sep-15 6:28 
QuestionMOre than one row in post data is not showing up in Auth Pin
mexidamus7-Aug-15 8:43
membermexidamus7-Aug-15 8:43 
QuestionWhy to use POST, not GET? Pin
ali.adravi22-Jul-15 5:39
memberali.adravi22-Jul-15 5:39 
Questionworking fyn Pin
Rohith Kr6-Mar-15 8:22
memberRohith Kr6-Mar-15 8:22 
Questioncrd operations Pin
Member 91711281-Jul-14 10:25
memberMember 91711281-Jul-14 10:25 
QuestionSoapAction in RESTful WCF service API using POST Pin
riya__yari9-May-14 22:25
memberriya__yari9-May-14 22:25 
GeneralCREATE RESTful WCF Service API Using Post... Wonderful guide! Pin
LisaSnyder12-Mar-14 5:41
memberLisaSnyder12-Mar-14 5:41 
Question[My vote of 1] : Not very good Pin
msdevtech6-Sep-13 17:05
membermsdevtech6-Sep-13 17:05 
QuestionUnable to POST - Method Now Allowed Pin
Member 1006724928-May-13 22:07
memberMember 1006724928-May-13 22:07 
GeneralMy vote of 4 Pin
rajeshpmarathe22-Feb-13 1:16
memberrajeshpmarathe22-Feb-13 1:16 
Questionhow to call XMl file contain different tags Pin
Member 98123935-Feb-13 20:41
memberMember 98123935-Feb-13 20:41 
GeneralMy vote of 5 Pin
eds.net28-Jan-13 6:46
membereds.net28-Jan-13 6:46 
GeneralMy vote of 5 Pin
SuperDuperPengu14-Jan-13 17:20
memberSuperDuperPengu14-Jan-13 17:20 
GeneralMy vote of 1 Pin
Member 813298511-Jul-12 21:48
memberMember 813298511-Jul-12 21:48 
GeneralHow to call xml directly Pin
Alpesh Dhanesha7-Jul-12 22:34
memberAlpesh Dhanesha7-Jul-12 22:34 
GeneralRe: How to call xml directly Pin
Member 98123935-Feb-13 20:54
memberMember 98123935-Feb-13 20:54 
Questionnot working Pin
mahesh152919-Apr-12 6:46
membermahesh152919-Apr-12 6:46 
AnswerRe: not working Pin
ashutosh k. shukla19-Apr-12 6:59
memberashutosh k. shukla19-Apr-12 6:59 
QuestionAshutosh need your help soon Pin
Member 884729719-Apr-12 6:35
memberMember 884729719-Apr-12 6:35 
AnswerRe: Ashutosh need your help soon Pin
ashutosh k. shukla19-Apr-12 7:01
memberashutosh k. shukla19-Apr-12 7:01 
GeneralRe: Ashutosh need your help soon Pin
Member 884729719-Apr-12 7:17
memberMember 884729719-Apr-12 7:17 
GeneralRe: Ashutosh need your help soon Pin
Member 884729719-Apr-12 7:20
memberMember 884729719-Apr-12 7:20 
QuestionAgain I need your Help Please (its urgent for me) Pin
MaheshRastogi00513-Feb-12 4:40
memberMaheshRastogi00513-Feb-12 4:40 
Questionparameter in url with out question mark Pin
MaheshRastogi00512-Feb-12 3:57
memberMaheshRastogi00512-Feb-12 3:57 
QuestionIs there a way to test a post directly from the URL of a browser? Pin
littleGreenDude3-Feb-12 10:58
memberlittleGreenDude3-Feb-12 10:58 
AnswerRe: Is there a way to test a post directly from the URL of a browser? Pin
ashutosh k. shukla3-Feb-12 18:39
memberashutosh k. shukla3-Feb-12 18:39 
SuggestionRe: Is there a way to test a post directly from the URL of a browser? Pin
ps3645210-Feb-12 1:15
memberps3645210-Feb-12 1:15 
GeneralRe: Is there a way to test a post directly from the URL of a browser? Pin
ashutosh k. shukla10-Feb-12 1:17
memberashutosh k. shukla10-Feb-12 1:17 
Questionplease help me its urgent Pin
MaheshRastogi0051-Feb-12 5:21
memberMaheshRastogi0051-Feb-12 5:21 
AnswerRe: please help me its urgent Pin
ashutosh k. shukla1-Feb-12 5:24
memberashutosh k. shukla1-Feb-12 5:24 
GeneralRe: please help me its urgent Pin
MaheshRastogi0051-Feb-12 5:28
memberMaheshRastogi0051-Feb-12 5:28 
GeneralRe: please help me its urgent Pin
ashutosh k. shukla1-Feb-12 5:28
memberashutosh k. shukla1-Feb-12 5:28 
QuestionCan I consume REST WCF via iPhone web app. Pin
kartikps_19795-Dec-11 11:54
memberkartikps_19795-Dec-11 11:54 
AnswerRe: Can I consume REST WCF via iPhone web app. Pin
ashutosh k. shukla5-Dec-11 17:17
memberashutosh k. shukla5-Dec-11 17:17 
QuestionGreat example Pin
Naresh Mittal16-Nov-11 18:14
memberNaresh Mittal16-Nov-11 18:14 
AnswerRe: Great example Pin
ashutosh k. shukla17-Nov-11 8:59
memberashutosh k. shukla17-Nov-11 8:59 
Questioncan RequestData have more than one data contract? Pin
Selveia15-Nov-11 5:27
memberSelveia15-Nov-11 5:27 
AnswerRe: can RequestData have more than one data contract? Pin
Naresh Mittal16-Nov-11 18:18
memberNaresh Mittal16-Nov-11 18:18 
QuestionThe remote server returned an error: (405) Method Not Allowed. Pin
prof.seva13-Nov-11 10:53
memberprof.seva13-Nov-11 10:53 
AnswerRe: The remote server returned an error: (405) Method Not Allowed. Pin
ashutosh k. shukla13-Nov-11 16:50
memberashutosh k. shukla13-Nov-11 16:50 
GeneralRe: The remote server returned an error: (405) Method Not Allowed. Pin
Selveia14-Nov-11 0:19
memberSelveia14-Nov-11 0:19 
QuestionThanks and keep it up Pin
RupeshKumar20-Oct-11 6:38
memberRupeshKumar20-Oct-11 6:38 
QuestionCreate a POST to a REST WCF Service with Fiddler Pin
Member 831979718-Oct-11 9:54
memberMember 831979718-Oct-11 9:54 
QuestionPassing Derived Class in RESTFul WCF Service Pin
Alexander B. Deyto12-Sep-11 20:01
memberAlexander B. Deyto12-Sep-11 20:01 
AnswerRe: Passing Derived Class in RESTFul WCF Service Pin
ashutosh k. shukla12-Sep-11 22:11
memberashutosh k. shukla12-Sep-11 22:11 
GeneralRe: Passing Derived Class in RESTFul WCF Service Pin
Alexander B. Deyto13-Sep-11 20:46
memberAlexander B. Deyto13-Sep-11 20:46 
QuestionExcellent Pin
Sunasara Imdadhusen31-Aug-11 20:03
memberSunasara Imdadhusen31-Aug-11 20:03 
AnswerRe: Excellent Pin
ashutosh k. shukla31-Aug-11 20:57
memberashutosh k. shukla31-Aug-11 20:57 
QuestionThanks Pin
hariharan.m8521-Aug-11 21:14
memberhariharan.m8521-Aug-11 21:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.151120.1 | Last Updated 25 May 2011
Article Copyright 2011 by ashutosh k. shukla
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid