Search code examples
apirestful-architecture

Changing/appending request headers in RESTful API in c#


I have a really weird situation (may be its for me only). I developed a RESTful API. By default it returns the result as JSON/XML/TEXT as per the Content Type sent by the client in headers.

Now client is saying that he wants to set the response as default as XML only. What I mean here is that client will not send any content type in headers and it will by default send the request as XML.

When I access this API from browser, it return it as XML but when client's app requests it, it returns JSON result by default. They are getting the result as XML by putting the content type in headers but they don't want to do it and want to have XML result by default.

I hope I am clear on it. If not please let me know.

Any help would be appreciated. Thanks

[Change] I am interested in knowing if there is some way I can modify the request headers when I receive request on server.

It is in MVC3, C#.


Solution

  • I find the answer and posting here. I just removed the other return types except the xml type like below:

    void ConfigureApi(HttpConfiguration config)
    {
        // Remove the JSON formatter
        config.Formatters.Remove(config.Formatters.JsonFormatter);
    
        // or
    
        // Remove the XML formatter
        config.Formatters.Remove(config.Formatters.XmlFormatter);
    }
    

    For more details, please follow below link http://www.asp.net/web-api/overview/formats-and-model-binding/json-and-xml-serialization

    Thanks