none
The incoming message has an unexpected message format 'Raw'. The expected message formats for the operation are 'Xml', 'Json'. RRS feed

  • Question

  • Hi All,

    I am getting this error and could not able to resolve this for the last two days and going crazy. I am trying to call WCF REST Service via JSON JQuery. I can able to call sucessfully The first service  GetFiles(string path) via GET  but not the second one via POST. Can anyone shed some light on this?

    Thanks a lot,

    Holy

    The code is as follows,

    WCF Service Interface  (Framework 3.5)

        [ServiceContract]
        interface IDataService
        {
            [OperationContract()]
            [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "{path}")]
            List<FileData> GetFiles(string path);
    
            [OperationContract()]
            [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json)]
            List<FileData> GetFilesViaPost(string path);
        }
    The Web.config Part for WCF

        <services>
          <service behaviorConfiguration="defaultServiceBehavior" name="Courses.Application.JQuery.Startup.DataService">
            <endpoint address="GetFiles" behaviorConfiguration="jsonBehaviour"
              binding="webHttpBinding" contract="Courses.Application.JQuery.Startup.IDataService" />
          </service>
        </services>
        <behaviors>
          <endpointBehaviors>
            <behavior name="jsonBehaviour">
              <webHttp  />
            </behavior>
          </endpointBehaviors>
          <serviceBehaviors>
            <behavior name="defaultServiceBehavior">
              <serviceMetadata httpGetEnabled="true"/>
              <serviceDebug includeExceptionDetailInFaults="true"/>
            </behavior>
          </serviceBehaviors>
        </behaviors>

    And the JSON JQuery part

                    // Using the core $.ajax() method
                    $.ajax({
    
                        // the URL for the request
                        url: "http://localhost:1513/DataService.svc/GetFiles/GetFilesViaPost",
                        // the data to send (will be converted to a query string)
                        data: JSON.stringify({ path: 'Windows' }),
                        // whether this is a POST or GET request
                        type: "POST",
                        //contentType: "application/json",
                        // the type of data we expect back
                        dataType: "json",
                        // code to run if the request succeeds;
                        // the response is passed to the function
                        success: function(json) {
                            var table = "<table>"
                            $.each(json, function(i, item) {
                                table += "<tr><td>" + item.Name + "</td><td>" + item.LastModifiedDateTime + "</td></tr>";
                            })
                            $(table).insertAfter("#butJson")
                        },
    
                        // code to run if the request fails; the raw request and
                        // status codes are passed to the function
                        error: function(xhr, status) {
                            alert("Sorry, there was a problem with your call. Status - " + status);
                            $("#txtHtml").val(xhr.status + ' ' + xhr.statusText + ' ' + xhr.responseText);
                        },
                        // code to run regardless of success or failure
                        complete: function(xhr, status) {
                            alert("The request is complete!");
                        }
                    });
                });

     


    • Edited by Holysmoke Thursday, January 30, 2014 2:58 PM
    Thursday, January 30, 2014 2:58 PM

Answers

All replies

  • Hi

    Here is an update regarding the problem indicated above,

    I could able to resolve it after change the WCF Service Interface of the second operation to

            [OperationContract()]
            [WebInvoke(Method = "POST", BodyStyle=WebMessageBodyStyle.Wrapped,  ResponseFormat = WebMessageFormat.Json)]
            List<FileData> GetFilesViaPost(string path);

    and on the JQuery part,

                    // Using the core $.ajax() method
                    $.ajax({
    
                        // the URL for the request
                        url: "http://localhost:1513/DataService.svc/GetFiles/GetFilesViaPost",
                        // the data to send (will be converted to a query string)
                        data: JSON.stringify({ path: 'Windows' }),
                        // whether this is a POST or GET request
                        type: "POST",
                        // 
                        contentType: "application/json",
                        // the type of data we expect back
                        dataType: "json",
                        // code to run if the request succeeds;
                        // the response is passed to the function
                        success: function(json) {
                            var jsonResult = json.GetFilesViaPostResult;
                            var table = "<table>"
                            $.each(jsonResult, function(i, item) {
                                table += "<tr><td>" + item.Name + "</td><td>" + item.LastModifiedDateTime + "</td></tr>";
                            })
                            $(table).insertAfter("#butJson")
                        },
    
                        // code to run if the request fails; the raw request and
                        // status codes are passed to the function
                        error: function(xhr, status) {
                            alert("Sorry, there was a problem with your call. Status - " + status);
                            $("#txtHtml").val(xhr.status + ' ' + xhr.statusText + ' ' + xhr.responseText);
                        },
                        // code to run regardless of success or failure
                        complete: function(xhr, status) {
                            alert("The request is complete!");
                        }
                    });
                });

    But not sure why we're forced set to wrapped. This creates an additional code on client side to read the JQuery results with wrapperObject.

    var jsonResult = json.GetFilesViaPostResult

    I want to get rid of this. Please advice.

    Grazie,

    Holy


    • Edited by Holysmoke Thursday, January 30, 2014 3:31 PM
    • Proposed as answer by dns jinung Thursday, February 6, 2014 7:40 AM
    Thursday, January 30, 2014 3:29 PM
  • Hi,

    Because Wrapped can support multiple parameters and have some extra XML tags in the body

    Also check:
    http://ednortonengineeringsociety.blogspot.in/2010/10/passing-json-array-to-wcf-service.html

    • Marked as answer by Holysmoke Thursday, February 6, 2014 8:43 AM
    Thursday, February 6, 2014 7:45 AM
  • Hi,

    Thanks for the reply. But it my case it's just a single parameter. My doubt is that JSON WCF combination will work only with attribute set with BodyStyle=WebMessageBodyStyle.Wrapped?

    TIA,

    Holy

    Thursday, February 6, 2014 8:43 AM