0

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#.

Rupesh Yadav
  • 12,096
  • 4
  • 53
  • 70
Deepak Kumar
  • 672
  • 4
  • 15
  • 31

2 Answers2

0

You can't change the request headers, just query them.

I guess you return your result as a simple string in your controllers, isn't it?

And, you are switching between results depending on the contenttype you read from request, don't you?

What is the contenttype the client call come with?

UPDATE:

Look at this page:

http://aleembawany.com/2009/03/27/aspnet-mvc-create-easy-rest-api-with-json-and-xml/

It's a solution for a previous version of MVC, but it will give you an idea about the solution you need:

Adjust the action result depending on the request contenttype

Alex Pollan
  • 863
  • 5
  • 13
  • Hi Alex, No I am not queering them basically. I am returning just object. I do not convert it as per request headers. I cannot check what content-type are so thats why I put the question here so I can know if I can modify them or not. – Deepak Kumar Nov 20 '12 at 09:07
  • Look at this page: http://aleembawany.com/2009/03/27/aspnet-mvc-create-easy-rest-api-with-json-and-xml/ It's a solution for a previous version of MVC, but it will give you an idea about the solution you need: Adjust the action result depending on the request contenttype – Alex Pollan Nov 21 '12 at 08:35
  • Hi Alex, thanks for helping. I think I found it and I am writing it as an answer so other may find it useful. – Deepak Kumar Nov 22 '12 at 08:32
0

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

Deepak Kumar
  • 672
  • 4
  • 15
  • 31