Search code examples
c#wcfhttprestwcf-rest

Can the RESTful service set its request and response type from "Accept" property of the client


Can a RESTFul service's request and response be serverd dynamically based on the "Accept" property of the client

i.e. If the client can "Accept" application/xml then my service should automatically understand that the request and response should be of type xml and process the request accordingly.

Hence, I would not create another service for client who want request/response in json and the same service should be able to handle it.


Solution

  • Yes, in fact WCF can be configured to do this for you: WCF Web HTTP Formatting

    When enabled, automatic formatting chooses the best format in which to return the response. It determines the best format by checking the following, in order:

    1. The media types in the request message’s Accept header.
    2. The content-type of the request message.
    3. The default format setting in the operation.
    4. The default format setting in the WebHttpBehavior.

    Also, see the documentation for <webHttpEndpoint>, specifically the automaticFormatSelectionEnabled attribute.