locked
REST client using ChannelFactory, how to set request format to be json instead of XML RRS feed

  • Question

  • User-105856378 posted

    I use ChannelFactory to create rest rest client to consume my rest service. By default, the client send request in XML format. How can I make it to send request in JSON format?

    My service web.config has sopecified automaticFormatSelectionEnabled=true as follow

    <behaviors>
          <endpointBehaviors>
            <behavior>
              <webHttp automaticFormatSelectionEnabled="true" />
            </behavior>
          </endpointBehaviors>
        </behaviors>

    This means my service can accept both XML or JSON request. The question is how to make my client code to send request in JSON format? Here is my client code

     

    ChannelFactory<IIndexService> factory = null;

                IIndexService proxy = null;

                    factory = new ChannelFactory<IIndexService>(new WebHttpBinding(),

                        new EndpointAddress(_indexServiceUrl));

                    factory.Endpoint.Behaviors.Add(new WebHttpBehavior());

                    proxy = factory.CreateChannel();

                    Object ret = proxMethod(proxy);

     Thanks.

     

    Friday, June 6, 2014 12:23 PM

Answers

  • User-417640953 posted

      <webHttp automaticFormatSelectionEnabled="true" />

    This means my service can accept both XML or JSON request. The question is how to make my client code to send request in JSON format?

    Hi ningjunwang2,

    Thank you post the issue to asp.net forum.

    When you set the rest service endpoint behavior automaticFormatSelectionEnabled as "true". That means the WCF infrastructure

    parses the Accept header of the request message and determines the most appropriate response format. If the Accept header

    does not specify a suitable response format, the WCF infrastructure uses the Content-Type of the request message or the default

    response format of the operation. It used to decide the format WCF response message not the accept format.

    As for how to define the accept format, we can set it in the service operation using RequestFormat attribute like below.

            [OperationContract]
            [WebInvoke(Method = "POST", UriTemplate = "XMLDataPost", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] 
            public int XMLDataPost(List<InquiryData> dd) 

    Thanks.

    Regards!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 9, 2014 6:13 AM

All replies

  • User-417640953 posted

      <webHttp automaticFormatSelectionEnabled="true" />

    This means my service can accept both XML or JSON request. The question is how to make my client code to send request in JSON format?

    Hi ningjunwang2,

    Thank you post the issue to asp.net forum.

    When you set the rest service endpoint behavior automaticFormatSelectionEnabled as "true". That means the WCF infrastructure

    parses the Accept header of the request message and determines the most appropriate response format. If the Accept header

    does not specify a suitable response format, the WCF infrastructure uses the Content-Type of the request message or the default

    response format of the operation. It used to decide the format WCF response message not the accept format.

    As for how to define the accept format, we can set it in the service operation using RequestFormat attribute like below.

            [OperationContract]
            [WebInvoke(Method = "POST", UriTemplate = "XMLDataPost", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] 
            public int XMLDataPost(List<InquiryData> dd) 

    Thanks.

    Regards!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 9, 2014 6:13 AM
  • User-105856378 posted

    Fuxiang

    Thanks for the response. My service contains 30 methods and I am tired of adding  RequestFormat = WebMessageFormat.Json to every methods. Is there any way to set the default request format to JSON?

    Wednesday, June 11, 2014 4:21 PM
  • User-417640953 posted

    My service contains 30 methods and I am tired of adding  RequestFormat = WebMessageFormat.Json to every methods. Is there any way to set the default request format to JSON?

    Hi ningjunwang2,

    Thanks for your back.

    Similar issue about this, you can refer to below for some enlightenment.

    http://stackoverflow.com/questions/11786281/how-to-set-the-default-requestformat-for-a-wcf-servicecontract

    Regards!

    Wednesday, June 11, 2014 10:35 PM
  • User-105856378 posted

    Fuxiang

    Thanks for your response. I looked at the link you posted and it seeems to say that it is up to the client to decide what format of request to send. So my question is how can my client code to set JSON as the request format? Here is my client code

     

       IIndexService proxy = null;

                    factory = new ChannelFactory<IIndexService>(new WebHttpBinding(),

                        new EndpointAddress(_indexServiceUrl));

                    factory.Endpoint.Behaviors.Add(new WebHttpBehavior());

                    proxy = factory.CreateChannel();

                    Object ret = proxMethod(proxy);

    Where in the above code can I tell it to use JSON instead of XML?

     

     

     

    Thursday, June 12, 2014 10:13 AM
  • User-417640953 posted

    IIndexService proxy = null;

                    factory = new ChannelFactory<IIndexService>(new WebHttpBinding(),

                        new EndpointAddress(_indexServiceUrl));

                    factory.Endpoint.Behaviors.Add(new WebHttpBehavior());

    Hi,

    As for you want to create a proxy using ChannelFactory for rest service. That must share the service contract interface

    between server and client side. If your client side owns the service contract, then it will create a proxy method which will send the

    parameters as the json or xml format complete decided by the operation WebInvoke attribute.

     [OperationContract]
            [WebInvoke(Method = "POST", UriTemplate = "XMLDataPost", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] 
            public int XMLDataPost(List<InquiryData> dd) 

    Besides, there are some threads about create proxy for rest service, I suggest you take a look below.

    http://stackoverflow.com/questions/6023265/create-a-wcf-proxy-for-a-rest-web-service

    http://blogs.msdn.com/b/pedram/archive/2008/04/21/how-to-consume-rest-services-with-wcf.aspx

    Thanks.

    Regards!

    Thursday, June 12, 2014 11:27 PM