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 :-(
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With