Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my C# client, POSTing to my WCF REST service, return (400) Bad Request?

Tags:

rest

c#

post

wcf

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 :-(

like image 544
Rob Avatar asked Feb 22 '09 21:02

Rob


People also ask

Why does C drive keeps filling up?

If your C drive is filling up without a reason, it can be due to a malware attack, file system corruption etc. The C drive is usually taken as the System partition on a computer system. System volume is the partition where your Windows is installed and where all the third-party programs intend to install by default.

What does this exclamation mark icon mean on my drive C?

A yellow triangle with exclamation mark is displayed on the C: drive in File Explorer. This is due to automatic device encryption not being enabled. The issue occurs if a user creates and logs on using a local account during the initial set up (out-of-box) of the computer.


2 Answers

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.

like image 194
Darrel Miller Avatar answered Oct 23 '22 12:10

Darrel Miller


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

like image 41
2 revs Avatar answered Oct 23 '22 10:10

2 revs