Switzerland's Citroen replica watches uk, although very good, but in replica watches popularity and Tissot is rolex replica immeasurably, but in fact it and Tissot belong to the rolex replica uk same Swatch Group, its positioning higher than the Tissot, lower than Hamilton.
[RestSharp] and RestClient’s ContentTypes « josemiguel.torres

[RestSharp] and RestClient’s ContentTypes

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.

   1: //endpoint: http://www.someurl.com/someResource/orWhatever
   2: private void RestSample() 
   3: {
   4:     var client = new RestClient
   5:     {
   6:         BaseUrl = "http://www.someurl.com/"
   7:     };
   9:     var request = new RestRequest
  10:     {
  11:         Resource = string.Format("someResource/orWhatever")
  12:     };
  14:     client.ExecuteAsync(request, (a) =>
  15:     {
  16:         var serializer = new XmlSerializer(typeof(MyFeedType));
  17:         if (a.StatusCode == HttpStatusCode.OK)
  18:         {
  19:             var document = XDocument.Parse(a.Content); //let's play
  20:         }
  21:         else
  22:         {
  23:             //something was wrong
  24:         }
  25:     });
  26: }

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:

   1: client.RemoveHandler("application/json"); 

Maybe there would be another way to achieve it, if so and you know it, drop me a comment.

, , , ,

Comments are closed.