restsharp / RestSharp

Simple REST and HTTP API Client for .NET
https://restsharp.dev
Apache License 2.0
9.57k stars 2.34k forks source link

When the content-type is not corresponding to the data #205

Closed FranciscoGileno closed 12 years ago

FranciscoGileno commented 12 years ago

Hi,

I have a situation where I request data from a url, the context-type is text/xml, but the data returned is text/json.

As showed on Wiki, I tried to override the default deserializer text/xml doing this:

        client.ClearHandlers();
        client.AddHandler("text/xml", new RestSharp.Deserializers.JsonDeserializer());

But I haven't got my class populated.

The Data returned from the url: http://www.swellmap.com/ajr.php?r=plugin&s=mana-island&a=Boating

Result:

HTTP/1.1 200 OK Date: Thu, 19 Jan 2012 22:30:39 GMT Server: Apache/1.3.34 Ben-SSL/1.55 (Ubuntu) PHP/5.2.3-0.dotdeb.1 with Suhosin-Patch AuthMySQL/4.3.9-2 mod_perl/1.29 Vary: Host X-Powered-By: PHP/5.2.3-0.dotdeb.1 Set-Cookie: PHPSESSID=d547cab5e3918d8fc192492024582c24; path=/ Expires: Sat, 1 Jan 2000 00:00:00 GMT Cache-Control: no-cache, must-revalidate Pragma: no-cache Last-Modified: Thu, 19 Jan 2012 22:30:39GMT Transfer-Encoding: chunked Content-Type: text/xml; charset=utf-8

31f ({"forecastdate":"12h","rating":"<img src='http:\/\/www.swellmap.com\/style\/img\/weathericons\/6r.png' alt='Average conditions' title='Average conditions' \/>","summary":"<img src='http:\/\/www.swellmap.com\/style\/img\/weathericons\/cloud.png' title='Cloudy' \/>","tides":"","seatemp":"-273°C","title":"mana-island","smaplink":"http:\/\/www.swellmap.com\/boating\/\/mana-island","vars":{"bhs":{"value":"1.3","title":"Sea","unit":"m"},"bhssw":{"value":"0.9","title":"Swell","unit":"m"},"hs_sea":{"value":"0.9","title":"Chop","unit":"m"},"tp":{"value":"8","title":"Period","unit":"s"},"dpm":{"value":"E","title":"Swell dir","unit":""},"dpm_sea":{"value":"--","title":"Sea dir","unit":""},"windma":{"value":"N 5","title":"Wind","unit":"kts"},"gstma":{"value":"6","title":"Gusts","unit":"kts"}}}) 0

To make it simple, for while, my class is just:

public class WeatherData
{
    public string ForecastDate { get; set; }
}
FranciscoGileno commented 12 years ago

Actually,

There are two problems here.

The first one, there is a extra "()" at the response data, and this is not a Restshaper's problem.

The second problem, I think is a Restshaper's bug, when you set a new custom deserializer as I did:

client.AddHandler("text/xml", new RestSharp.Deserializers.JsonDeserializer());

This throw a RestShaper's exception about Newtonsoft.Json versioning.

I resolved this issue doing this at app.config:

    <assemblyIdentity name="Newtonsoft.Json"
                      publicKeyToken="30ad4fe6b2a6aeed"
                      culture="neutral" />
    <!-- Assembly versions can be redirected in application, publisher policy, or machine configuration files. -->
    <bindingRedirect oldVersion="0.0.0.0-4.9.9.9" newVersion="4.0.5.0"/>

PS: I got Restshaper through Nuget about 3 days ago.

johnsheehan commented 12 years ago

The NuGet issue should be resolved. If you still have issues, post to http://groups.google.com/group/restsharp