HTTP / 1.1 415 Cannot process the message because the content type 'application / json; charset = utf-8' was not the expected type text / xml; encoding = UTF-8 '

trying to use a POST json dictionary for C # WCF when I call its HTTP response 415. Someone might tell me what is wrong with my code.

object class

[DataContract] public class Class1 { [DataMember] public string AccNo; [DataMember] public string CompanyName; [DataMember] public string DocDate; } 

IService1.cs

  [OperationContract] [WebInvoke(Method = "POST", UriTemplate = "json/PostSalesOrderData", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)] string PostSalesOrderData(string data); 

Service1.svc.cs

  public string PostSalesOrderData(string data) { JavaScriptSerializer serializer = new JavaScriptSerializer(); Dictionary<string, Class1> dict = serializer.Deserialize<Dictionary<string, Class1>>(data); return dict["Debtor"].AccNo.ToString(); } 

Script details

HTTP / 1.1 415 Cannot process the message because the content type 'application / json; charset = utf-8' was not the expected type text / xml; encoding = UTF-8 '. Server: Microsoft-IIS / 7.5 X-Powered- By: ASP.NET Date: Thu, 29 Nov 2012 01:21:55 GMT Content-Length: 0

+6
source share
1 answer

The endpoint of your service is not configured correctly to accept JSON input. In order for the [WebInvoke] attribute to execute, your endpoint must have webHttpBinding , and it must also have endpoint behavior like <webHttp/>

One easy way to ensure that it is configured correctly is to use the Factory attribute in the .svc file. Something like the example below:

 <%@ ServiceHost Language="C#" Debug="true" Service="YourNamespace.YourServiceClass" Factory="System.ServiceModel.Activation.WebServiceHostFactory" %> 
+12
source

All Articles