tags:

views:

4910

answers:

3

I'm trying to send a POST request to a simple WCF service I wrote, but I keep getting a 400 Bad Request. I'm trying to send JSON data to the service. Can anyone spot what I'm doing wrong? :-)

This is my service interface:

public interface Itestservice
{
    [OperationContract]
    [WebInvoke(
        Method = "POST",
        UriTemplate = "/create",
        RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json)]
    String Create(TestData testData);
}

The implementation:

public class testservice: Itestservice
{
    public String Create(TestData testData)
    {
        return "Hello, your test data is " + testData.SomeData;
    }
}

The DataContract:

[DataContract]
public class TestData
{
    [DataMember]
    public String SomeData { get; set; }
}

And finally my client code:

private static void TestCreatePost()
{
    Console.WriteLine("testservice.svc/create POST:");
    Console.WriteLine("-----------------------");

    Uri address = new Uri("http://localhost:" + PORT + "/testweb/testservice.svc/create");

    // Create the web request  
    HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;

    // Set type to POST  
    request.Method = "POST";
    request.ContentType = "application/x-www-form-urlencoded";
    //request.ContentType = "text/x-json";

    // Create the data we want to send  
    string data = "{\"SomeData\":\"someTestData\"}";

    // Create a byte array of the data we want to send  
    byte[] byteData = UTF8Encoding.UTF8.GetBytes(data);

    // Set the content length in the request headers  
    request.ContentLength = byteData.Length;

    // Write data  
    using (Stream postStream = request.GetRequestStream())
    {
        postStream.Write(byteData, 0, byteData.Length);
    }

    // Get response  
    using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
    {
        // Get the response stream  
        StreamReader reader = new StreamReader(response.GetResponseStream());

        // Console application output  
        Console.WriteLine(reader.ReadToEnd());
    }

    Console.WriteLine();
    Console.WriteLine();
}

Can anyone think of what I might be doing wrong? As you can see in the C# client I've tried both application/x-www-form-urlencoded and text/x-json for ContentType, thinking that might have something to do with it, but it doesn't seem to. I've tried a GET version of this same service and it works fine, and returns a JSON version of TestData with no problem. But for POST, well, I'm pretty stuck at the moment on this :-(

A: 

Try:

[OperationContract]
[WebInvoke(
  Method = "POST",
  UriTemplate = "/create",
  RequestFormat = WebMessageFormat.Json,
  ResponseFormat = WebMessageFormat.Json,
  /* non-wrapped */ BodyStyle = WebMessageBodyStyle.Bare )]
String Create(TestData testData);
baretta
it only takes one parameter. does it really matter if it is wrapped or not? In this case the server rejects the request because the contentType in the request is not desired.
codemeit
yea the contentType is wrong, too. But i had this problem myself so i was pretty sure it was the BodyStyle causing this.
baretta
+2  A: 

Have you tried "application/json" instead of "text/x-json". According to this Stack Overflow question application/json is the only valid json media type.

Darrel Miller
That was it, thanks :-)
unforgiven3
+1  A: 

The only problem here is the ContentType.

try ( recommended )

request.ContentType = "application/json; charset=utf-8";

or ( this will work too )

request.ContentType = "text/json; charset=utf-8";

Both of above solve the problem. However, the first one is recommended, for details of JSON-RPC 1.1 Specification check out http://json-rpc.org

codemeit