Here’s the first post. While this first post on my iPhone development blog has nothing to do with the iPhone, it’s something that I’d like to share with you all as it took me a few hours to get this one totally figured out.
The first post I’ll share is a scenario of a problem I was having with communicating with a RESTful WCF service. For some reason all I was getting back from the service was an HTTP 400 – Bad Request response when attempting to make a POST to a url.
So, here’s what I was attempting to do. I created a simple WCF RESTful web service. This web service will be used for all the communication between the iPhone’s and my server.
I created a simple method defined as follows:
Looks strait forward enough. This definition means that the method will respond to POST actions against the url /users. The RequestFormat is XML and the ResponseFormat is XML. The message format of the body of the request is Bare, meaning that it will just contain the raw XML.
Ok, now that the method is defined and I waived some magic under the hood of the interface definition to do something with the request. I bring up my trusty HTTP application to send some requests to the service and see what happens. (the trusty HTTP app I was using if you were wondering is: HTTP Client). Now, I spin up and send some requests to the service.
Firstly, you need to make sure that you specify the content type and a couple other headers needed for the requests.
That’s it, end of story, Go to Jail, Do Not Pass Go!! WCF does not give you any error messages, nothing, NADA! How do you figure out what’s wrong?
I spent a lot of time searching on the web, trying to find out, is it an error in definition of the method in the service, is it hitting the service at all, put a breakpoint in the service, nothing. The error is being thrown by the WCF runtime, so what gives?? What’s the real error?
After trying everything I could think of I did the one thing that actually makes sense. I went into my application, and wrote a UNIT TEST. I started from the beginning, create valid XML representing the DataContract object I want to send through the WCF service.
Voila, problem solved! The problem turned out to be that I wasn’t specifying the namespace on the XML representation of the object I was sending in the body of the POST message!! Now, why couldn’t WCF have told me that? Why not give me a friendly error message in the response (I have the setting enabled to show full error details in the WCF XML configuration in the web.config file).
Just include the namespace on the XML object I was sending in the body of the request. So, with the added namespace(xmlns=“http://development.svnanalytics.com/contracts” xmlns:i=“http://www.w3.org/2001/XMLSchema-instance”) Everything just worked again!
Here’s the working Request and Response. Note the namespace now specified on the XML in the body: