My life is easier since I met RestSharp library. Since then, all my Windows Phone app use such library, and excluding some issues on behalf of Newtonsoft.Json version, the truth is that RestSharp is quite helpful. Well, one of the issues I would like to talk about is concerning to the Content Types that RestSharp supports. There are 6 types of content and we may find them in AcceptTypes property of the RestClient object.
In the following example, we aim to achieve some kind of xml based data and perform some actions like xml deserialization.
We assume that this endpoint provides xml content and therefore this snippet code will work correctly. The issue here is when that endpoint does not explicity address to a given content type and supports more than one content type. Let’s say this endpoint provides both “application/xml” and “application/json”. Then the code above will not work since the a.Content given from the request will contain json based content. Well, we may use JsonDeserializer class in order to turn this json string into a C# objects, however I would like to share with you the reason because it happens.
The reason comes from the RestClient’s AcceptTypes property I pointed out above. If we breakpoint client object before the call we can see the 6 content types that RestSharp providers per default.
RestClient will try the first one which is “application/json” and as the endpoint provides json then the RestClient object call named client will retrieve json content in the RestRequest callback.
The only way I figured out in order to force explicity xml call is by removing the “application/json” content as follows:
Maybe there would be another way to achieve it, if so and you know it, drop me a comment.