11

I want to send data in jason format to a wcf service for processing. Wcf service is developed and when jason input is sent to the service using fiddler, it throws the error - The server encountered an error processing the request. The exception message is '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.'. See server logs for more details.

Service contract
================

 public interface IRegisterEmployee
    {

        [OperationContract]
        [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, BodyStyle=WebMessageBodyStyle.Bare, ResponseFormat=WebMessageFormat.Json, UriTemplate = "AddEmployee")]
        bool ProcessEmployee(Employee emp);
    }

   [DataContract]
    public class Employee
    {
        [DataMember]
        public string emp { get; set; } //this is actually a complex type, but simplified here

    }

Service class
============
public class RegisterEmployee : IRegisterEmployee
    {
        public bool ProcessEmployee(Employee emp)
        {
            //do some processing
            return true;

        }

Web.config
=========
<services>
      <service name="Project.RegisterEmployee">
        <endpoint address="Rest" behaviorConfiguration="RestfulBehavior" binding="webHttpBinding" name="Rest" contract="Project.IRegisterEmployee" />
        <endpoint address="Soap" behaviorConfiguration="" binding="basicHttpBinding" name="Soap" contract="Project.IRegisterEmployee" />
        <endpoint address="Mex" behaviorConfiguration="" binding="mexHttpBinding" name="Mex" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost/Project" />
          </baseAddresses>
        </host>
      </service>
    </services>

    <endpointBehaviors>
        <behavior name="RestfulBehavior">
          <webHttp automaticFormatSelectionEnabled="true" />
        </behavior>
    </endpointBehaviors>

*Fiddler
======
POST;  http://localhost/Project/RegisterEmployee.svc/Rest/AddEmployee
Content-Type: application/jason
Request Body = {"emp" : "test"}*

Error - HTTP/1.1 400 Bad Request

If I use wcftestclient (debug mode), it works fine - guess it uses soap/xml.

bdotnet
  • 343
  • 1
  • 4
  • 16
  • This message also comes up if you put Accept: application/json when you meant to put Content-Type: application/json – smoore4 Sep 15 '20 at 21:14

2 Answers2

22

The content type of the request should be application/json, not application/jason. Try changing that and it should work.

carlosfigueira
  • 85,035
  • 14
  • 131
  • 171
  • I'm facing one more issue - the input parameter for the method is a complex type (I have simplified it in the above code though). Employee class contains an array of another complex datatype (class) which in turn has an array of another complex type (class). When I send Json input, I see the values of the 1st and 2nd level but 3rd level values are showing empty. Is there any limitations on nesting level? It works fine when I use soap/xml – bdotnet Oct 01 '14 at 08:18
0

This issue will still occur after the above correction IF there is a mismatch between what the content type mapper is returning and the RequestFormat.

On your binding, if you have

contentTypeMapper="Abc.Service.NewtonsoftCustom.CustomContentTypeMapper

verify that what the method

CustomContentTypeMapper(string contentType)

returns matches say

RequestFormat =WebMessageFormat.Json 

on your operationcontract

jordan koskei
  • 2,749
  • 1
  • 15
  • 12