Solution to HTTP 400 Bad Request when POSTing to WCF RESTful web service

Standard

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.

REQUEST

RESPONSE

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).

Resolution

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:

Request

Response

2 thoughts on “Solution to HTTP 400 Bad Request when POSTing to WCF RESTful web service

  1. Deeps

    Hi, I am getting bad request error while trying to access my WCF service from iphone end. The data is in json format. What should be possible cause. Please reply.

    • deanpoulin

      There’s many reasons you could be getting a bad request on the WCF side. Are you sending a POST request with JSON in the body?

      1. Make sure the JSON in the body is properly formatted.
      2. Check the Attributes on the WCF Service Method
      3. Make sure the service method is a POST vs GET
      4. Make sure you’re setting the iPhone Objective-C POST request header for the content type to JSON:

        • [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

      I’d recommend using Charles to proxy all your traffic from your iPhone through your computer so you can see the raw HTTP request the iPhone is sending. Check this out: Using Charles from an iPhone.

      This will help you debug and find the request that works then get the iPhone to generate the correct request.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s