Not a subscriber?

Join thousands of others who are building self-directed lives through creativity, grit, and digital strategy—breaking free from the 9–5.
Receive one free message a week

How To: REST Services in WCF 3.5 Part 2 – The POST

This post is part of a series.

 

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-urlencoded

userid=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)

image 

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:

image

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:

  1. Install Fiddler
  2. Open Fiddler
  3. Open the request builder
  4. Select Post
  5. Build the request
  6. Submit the request

3. Open the request builder

image

 

4. Select POST

image

 

5. Build The Request

 image

Like this:

image

 

6. Submit the request

image

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.

image

 

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:

 

Download Code

Part2.zip (~11kb)