locked
Problem with send WCF POST request with Content-type: application/json RRS feed

  • Question

  • Hello, I currently working with a restful wcf service under webHttp. And I have a same problem as this topic: http://social.msdn.microsoft.com/Forums/en/wcf/thread/99e71c23-00ae-495b-8c6d-8100592c2444

    I try everything, but it not works :(

    Here's my code:

    Webconfig:

    <service name="MobileServices.DepositCardService" behaviorConfiguration="myServiceBehavior">
            <endpoint binding="webHttpBinding" contract="MobileServices.IDepositCardService" behaviorConfiguration="web"/>
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
          </service>


    <endpointBehaviors>
            <behavior name="web">
              <webHttp/>
            </behavior>
          </endpointBehaviors>

    and here's contract:

    [ServiceContract]
        public interface IDepositCardService
        {
            [OperationContract]
            [WebInvoke(Method = "POST",
                RequestFormat = WebMessageFormat.Json,
                ResponseFormat = WebMessageFormat.Json,
                BodyStyle = WebMessageBodyStyle.Bare,
                UriTemplate = "DepositCard")]
            DepositResponseData DepositCard(DepositRequestData depositData);
        }

    I have receive a response that:

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

    Please help me!!!

    Tks.




    • Edited by VietTD Tuesday, April 24, 2012 10:28 AM
    Tuesday, April 24, 2012 10:25 AM

Answers

  • The http 404 error illustrates that the server has not found anything matching the URI given, you need to make sure the  request URL is correct, you could try changing the method and web.config as follows:

            [OperationContract]
            [WebInvoke(Method = "POST",
                RequestFormat = WebMessageFormat.Json,
                ResponseFormat = WebMessageFormat.Json,
                BodyStyle = WebMessageBodyStyle.Bare,
                UriTemplate = "/DepositCard/")]
            DepositResponseData DepositCard(DepositRequestData depositData);

    <service name="MobileServices.DepositCardService" behaviorConfiguration="myServiceBehavior">
            <endpoint address="" binding="webHttpBinding" contract="MobileServices.IDepositCardService" behaviorConfiguration="web"/>
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
          </service>

    and specify WebServiceHostFactory in the markup code of .svc file. and set the request URL as follows on the client side:

    e.g: http://localhost/directorynameiniis/Service1.svc/DepositCard/

    As Rajesh S V metioned, you can post request message (request header and request body) from fiddler and see the response message. Here is a post to which you can refer.

    http://stackoverflow.com/questions/6617387/send-json-to-wcf-3-5-using-ajax


    Please mark the replies as answers if they help or unmark if not. If you have any feedback about my replies, please contact msdnmg@microsoft.com Microsoft One Code Framework

    Friday, April 27, 2012 5:40 AM

All replies

  • How are you trying to consume the above WCF Rest service? Also post DepositRequestData structure to give you on how your request should look like. Try to enable Tracing on your service to see the exact reason on why your call is failing.
    • Edited by Rajesh S V Tuesday, April 24, 2012 11:44 AM
    Tuesday, April 24, 2012 11:43 AM
  • DepositRequestData structure:

    [DataContract]
        public class DepositRequestData
        {
            [DataMember]
            public string CardCode { get; set; }
            [DataMember]
            public string CardPassword { get; set; }
            [DataMember]
            public int PaymentOption { get; set; }
            [DataMember]
            public string AccountName { get; set; }
            [DataMember]
            public string UserIP { get; set; }
            [DataMember]
            public string ServerID { get; set; }
            [DataMember]
            public string ExParams { get; set; }
        }

    But now I'm crazy with this problem:

    When I add this code in web.config, I have Exception : The remote server returned an error: (404) Not Found.

    <service name="MobileServices.DepositCardService" behaviorConfiguration="myServiceBehavior">
            <endpoint binding="webHttpBinding" contract="MobileServices.IDepositCardService" behaviorConfiguration="web"/>
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
          </service>

    When I comment it, I have Exception: The remote server returned an error: (415) Cannot process the message because the content type 'application/json' was not the expected type 'text/xml; charset=utf-8'

    This is my code to test the service:

    data.CardCode = "ABCDEFGH";
                    data.CardPassword = "123456789";
                    data.PaymentOption = 999;
                    data.AccountName = "myaccount";
                    data.UserIP = "xxx.xxx.xxx.xxx";
                    data.ServerID = "serverID";
                    data.ExParams = "";
    
                    System.Net.WebClient client = new System.Net.WebClient();
                    client.Headers["Content-type"] = "application/json";
                    MemoryStream ms = new MemoryStream();
                    DataContractJsonSerializer serializerToUpload = new DataContractJsonSerializer(typeof(MobileServices.DepositRequestData));
                    serializerToUpload.WriteObject(ms, data);
                    byte[] responsedata = client.UploadData(url, "POST", ms.ToArray());
    
                    Stream stream = new MemoryStream(responsedata);
                    DataContractJsonSerializer obj = new DataContractJsonSerializer(typeof(MobileServices.DepositResponseData));
                    MobileServices.DepositResponseData result = (MobileServices.DepositResponseData)obj.ReadObject(stream);

    Exception at this line:
    byte[] responsedata = client.UploadData(url, "POST", ms.ToArray());

    Wednesday, April 25, 2012 3:46 AM
  • An mex endpoint is not needed in the above scenario. Also have you tried to inspect your request using Fiddler when you perform a POST using the above code?

    Rajesh S V

    Wednesday, April 25, 2012 8:40 AM
  • Also try to post the below request from Fiddler and see what is the response:

    POST http://localhost/SampleApp/Service1.svc/DepositCard HTTP/1.1
    Content-Type: application/json
    Host: localhost

    {"CardCode":"myCardCode","CardPassword":"myCardPassword","PaymentOption":1,"AccountName":"Test account","UserIP":"127.0.0.1","ServerID":"localhost","ExParams":""}



    Rajesh S V

    Wednesday, April 25, 2012 9:14 AM
  • The http 404 error illustrates that the server has not found anything matching the URI given, you need to make sure the  request URL is correct, you could try changing the method and web.config as follows:

            [OperationContract]
            [WebInvoke(Method = "POST",
                RequestFormat = WebMessageFormat.Json,
                ResponseFormat = WebMessageFormat.Json,
                BodyStyle = WebMessageBodyStyle.Bare,
                UriTemplate = "/DepositCard/")]
            DepositResponseData DepositCard(DepositRequestData depositData);

    <service name="MobileServices.DepositCardService" behaviorConfiguration="myServiceBehavior">
            <endpoint address="" binding="webHttpBinding" contract="MobileServices.IDepositCardService" behaviorConfiguration="web"/>
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
          </service>

    and specify WebServiceHostFactory in the markup code of .svc file. and set the request URL as follows on the client side:

    e.g: http://localhost/directorynameiniis/Service1.svc/DepositCard/

    As Rajesh S V metioned, you can post request message (request header and request body) from fiddler and see the response message. Here is a post to which you can refer.

    http://stackoverflow.com/questions/6617387/send-json-to-wcf-3-5-using-ajax


    Please mark the replies as answers if they help or unmark if not. If you have any feedback about my replies, please contact msdnmg@microsoft.com Microsoft One Code Framework

    Friday, April 27, 2012 5:40 AM