This post is part of a series.
- Part 1
- Part 2 (this post)
Implementing a POST HTTP Verb Call
In this post we’re going to cover what it takes to implement a PUT into our REST Service that we defined in Part 1 of this series.
First of all, what is the POST HTTP verb for? To understand this lets take a look at how a HTTP Get call is formed from an HTTP Header perspective. A GET request fetches data from a web server based solely on a URL value and a set of HTTP headers.
HTTP GET Header Example:
GET /index.html?userid=joe&password=guessme HTTP/1.1
Host: www.example.org
User-Agent: Mozilla/4.0
In this example the Get requests from a host (www.example.org) with the Mozilla/4.0 type browser and is asking for the index.html page with the query string values userid and password (and their corresponding values).
Now for the HTTP POST… POST request sends additional data to the web server, specified after the URL, the headers, and a blank line to indicate the end of the headers. An example:
POST /login.aspx HTTP/1.1
Host: www.example.org
User-Agent: Mozilla/4.0
Content-Length: 27
Content-Type: application/x-www-form-urlencodeduserid=joe&password=guessme
At a very simple level – A POST is normally done from a web form. When we fill out a form and send the data the form attribute “method” is set to “post”.
If you want the super formal explanation of POST check out the POST definition on the W3C site.
The implementation in WCF
In the previous example we used a simple Gas Price Service. This next service is something a little different. Lets assume that we have a comedy web site that allows users to get “insults” and “save insults”. We are going to use this example, and its called the “Insult Service”. Its a very simple REST service that insults you or your co-workers or anyone else for that matter. Kind of fun to mess around with. This version of the insult service has the following methods:
- GET: Insult someone (an exact copy of the previous part 1, just in a different context)
- POST: Add a new insult
To receive an insult we ask the insult service to insult someone via the GET (again, this is the same as the previous example).You would call the service like this: http://localhost:7000/insult/{personsNameToInsult} .
NOTE: The insult service grabs a random insult from a list of insults and returns it to the user.
Here it is in action: Replace {personsNameToInsult} with “Donn” (minus the quotes)
Here is the GET in code from the WCF Service Contract:
[OperationContract] [WebGet(RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "/insult/{personToInsult}")] Insult GetInsult(string personToInsult);
Now, to implement the POST we need a way to tell the server that we want to POST data to a service. Here’s how we do it in WCF:
[OperationContract] [WebInvoke(RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "/insult/Add/{insultName}/{insultText}", Method = "POST")] void AddInsult(string insultName, string insultText);
In this service contract we are telling WCF that the URITemplate is going to be as such:
/insult/Add/{insultName}/{insultText}
Where {insultName} and {insultText} map to the string values in the “AddInsult” method. To add an insult we’d have a POST request that looks like this:
http://localhost:7000/insult/Add/TestInsult/This+is+a+test+insult
However, if we type this into the Browser window we will get the following:
This is because the web browser by default performs a GET operation on the URI that is posted into the address bar.
So how do we POST? We can either build a web form to do it for us, or we can use a web debugging tool.
Adding a New Insult: The POST
Since we do not have a web front end for this service we need to construct a HTTP POST manually. We can do this with Fiddler. Fiddler is a Web Debugging tool that allows you to inspect HTTP Traffic and construct requests. We’re going to use it for constructing our POST call. (You could also create a simple web form to do this as well).
Constructing the POST:
- Install Fiddler
- Open Fiddler
- Open the request builder
- Select Post
- Build the request
- Submit the request
…
3. Open the request builder
4. Select POST
5. Build The Request
Like this:
6. Submit the request
At this point our service has been called and the values have been written to the data repository (db or file, etc).
Now lets see if the insult shows up in our service. Lets hit refresh on our GET page a few times to see if it shows up. Which it should.
Conclusion
As you can see, its fairly simple to implement a POST action to add new values to a WCF REST service. This has been done utilizing the WebInvokeAttribute to handle our POST action.
Additional Reading:
John says
good.
Brian Carlson says
Great article, How would one go about accessing post values.
teddy d. says
Great article Donn – solved my problem. Thanks for posting!
Joshua McCullough says
You should also mention how to handle getting the post data out of the request body, since that is normally how you would pass data via the post method.
Pawan says
very good article, Resolve my stuck
cheap uggs says
MEYUA5UO76UXYED9SD
Young girls all have a crush on wearing the cheap ugg boots
haha says
authentic nfl jerseys Inexpensive items
coach bags on sale I am confident with his
coach outlet store online First step in success
coach outlet online Workmanship
coach bags outlet Value of goods
coach outlet store Of love
wholesale designer bags Beautiful lines
coach bags outlet There range of children
cheap coach What are you waiting
wholesale nfl jerseys Is always expected
authentic nfl jerseys It is pretty good
Coach Factory Outlet says
http://buycoachfactoryoutletsz.com
mservidio says
agreed. Could you possibly show how to handle getting the post data out of the request body?
ErNole says
Great “POST”, Resolve my Problem. ๐
bhargav says
HTTP/1.1 411 Length Required
Content-Type: text/html; charset=us-ascii
Server: Microsoft-HTTPAPI/2.0
Date: Wed, 08 May 2013 10:29:25 GMT
Connection: close
Content-Length: 344
Length Required
Length Required
HTTP Error 411. The request must be chunked or have a content length.
Eldho says
Great article , i would like to know how to pass class using fiddler in POST METHOD