Question

I am trying to build a REST & json based WCF service that takes a complex type as input. On the client I am trying to consume this service using HttpClient that comes as a part of WCF REST Starter Kit.

Below is my service code:

[WebInvoke(Method = "POST", UriTemplate = "/SendData", BodyStyle = WebMessageBodyStyle.Wrapped)]
public void SendData(List<EditorData> input)
{
//Do something
}

I have used other options that could be found in the WebMessageBodyStyle enum to no avail.

Here is my complex type data contract which I am using in my client as well:

public class EditorData
{
    public string key { get; set; }
    public long quesno { get; set; }
    public string quescontent { get; set; }
}

Client code:

List<EditorData> listEditor = new List<EditorData> { new EditorData { key = "key1", quescontent = "qcontent1", quesno = 1},new EditorData { key = "key2", quescontent = "qcontent2", quesno = 2}};
string jsonEditorList = listEditor.ToJSON();
HttpClient client = new HttpClient("http://localhost/RestWcfService/RestService.svc/");
client.DefaultHeaders.Accept.Add("application/json");
HttpResponseMessage response = null;
response = client.Post("SendData", HttpContent.Create(jsonEditorList));
response.EnsureStatusIsSuccessful();

To convert my custom object list to json string, I am using the extension method I found here

When I run this application I get the following error:

BadRequest (400) is not one of the following: OK (200), Created (201), Accepted (202), NonAuthoritativeInformation (203), NoContent (204), ResetContent (205), PartialContent (206)

Any thoughts?

EDIT:

Here is the fiddler screenshot:

enter image description here

UPDATE:

As suggested by Jason Freitas, I checked the response in fiddler. This is what is says:

The server encountered an error processing the request. See server logs for more details.

So I went into IIS logs and here is the error logged in IIS:

2012-02-15 13:20:08 fe80::ecdd:d2dd:7f70:bef6%11 POST /RestWcfService/RestService.svc/SendData - 80 - fe80::ecdd:d2dd:7f70:bef6%11 - 400 0 0 0

UPDATE 2

As per Rajesh's suggestion I enabled tracing for my wcf service. Below is the exception thrown by the server:

The incoming message has an unexpected message format 'Raw'. The expected message formats for the operation are 'Xml', 'Json'. This can be because a WebContentTypeMapper has not been configured on the binding. See the documentation of WebContentTypeMapper for more details.

I still don't understand how it is getting Raw format when I have specified content type as json.

Was it helpful?

Solution

First try to enable Tracing on your WCF Service to see the exact cause of the 400 bad request error.

Seems like the input being posted is in a wrong format. You have defined a list of EditorData as a parameter to the method and posting some key value pairs (referring to your fiddler screenshot) Make sure that the json string in fiddler when deserialized gets converted to list of EditorData objects.

Also you have set the body style to be wrapped. Try to remove that and see if it works. Wrapped requests are used when you have multiple parameters then in that case you wrap all the parameters inside the method name element and send it across the wire.

UPDATE:

Please make sure to add the Content-Type to the header as shown below:

client.DefaultHeaders.ContentType = "application/json";

OTHER TIPS

[WebInvoke] defaults to XML serialization. You need to tell it that you are sending the data as JSON. Set the RequestFormat in the WebInvoke attribute like this

RequestFormat = WebMessageFormat.Json

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top