Reputation: 17383
I've a code below updated from http://www.stum.de/2009/12/22/using-restsharp-to-consume-restful-web-services/. I've updated for latest version of RestSharp:
var client = new RestClient("http://carma.org");
var request = new RestRequest("api/1.1/searchPlants", Method.GET);
request.AddParameter("location", 4338);
request.AddParameter("limit", 10);
request.AddParameter("color", "red");
request.AddParameter("format", "xml");
var plants = client.Execute<PowerPlantsDTO>(request);
MessageBox.Show(plants.Count.ToString())
using System.Collections.Generic;
namespace RestTest.Model
{
public class CityDTO
{
public string value { get; set; }
}
public class LocationDTO
{
public CityDTO city { get; set; }
public int zip { get; set; }
public double latitude { get; set; }
public double longitude { get; set; }
}
public class item
{
public string name { get; set; }
public LocationDTO location { get; set; }
}
public class PowerPlantsDTO : List<item> { }
}
Unfortunately, plants.count is empty so as plants.data but there's a XML data returned (see screeshots below). Could someone please help me whether I'm missing anything?
Screenshots:
The Errmessage shows
"Parameter count mismatch."
And XML Content Returned:
<?xml version="1.0" encoding="UTF-8"?>
<items>
<item>
<id>49046</id>
<name>WATSON COGEN</name>
<carbon>
<past>4503176.0000</past>
<present>4582168.0000</present>
<future>5401482.0000</future>
</carbon>
<energy>
<past>3827727.0000</past>
<present>3017826.0000</present>
<future>3506896.0000</future>
</energy>
<intensity>
<past>2352.9250</past>
<present>3036.7339</present>
<future>3080.4910</future>
</intensity>
<location>
<continent>
<id>5</id>
<value>North America</value>
</continent>
<country>
<id>202</id>
<value>United States</value>
</country>
<latitude>33.8219</latitude>
<longitude>-118.2633</longitude>
<state>
<id>644</id>
<value>California</value>
</state>
<city>
<id>60769</id>
<value>Carson</value>
</city>
<metroarea>
<id>3203</id>
<value>Los Angeles-Long Beach</value>
</metroarea>
<county>
<id>4338</id>
<value>Los Angeles</value>
</county>
<congdist>
<id>5298</id>
<value>Diane Watson</value>
</congdist>
<zip>90749</zip>
</location>
</item>
<item>
<id>7233</id>
<name>CARSON COGEN</name>
<carbon>
<past>432223.9062</past>
<present>440564.3125</present>
<future>451224.5000</future>
</carbon>
<energy>
<past>461797.6875</past>
<present>348148.4062</present>
<future>355428.0938</future>
</energy>
<intensity>
<past>1871.9189</past>
<present>2530.8989</present>
<future>2539.0481</future>
</intensity>
<location>
<continent>
<id>5</id>
<value>North America</value>
</continent>
<country>
<id>202</id>
<value>United States</value>
</country>
<latitude>33.8759</latitude>
<longitude>-118.2491</longitude>
<state>
<id>644</id>
<value>California</value>
</state>
<city>
<id>60769</id>
<value>Carson</value>
</city>
<metroarea>
<id>3203</id>
<value>Los Angeles-Long Beach</value>
</metroarea>
<county>
<id>4338</id>
<value>Los Angeles</value>
</county>
<congdist>
<id>5433</id>
<value>Juanita Millender-McDonald</value>
</congdist>
<zip>90746</zip>
</location>
</item>
</items>
Stack Trace:
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)
at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, Object[] index)
at RestSharp.Deserializers.XmlDeserializer.Map(Object x, XElement root)
at RestSharp.Deserializers.XmlDeserializer.HandleListDerivative(Object x, XElement root, String propName, Type type)
at RestSharp.Deserializers.XmlDeserializer.Deserialize[T](IRestResponse response)
at RestSharp.RestClient.Deserialize[T](IRestRequest request, IRestResponse raw)
Upvotes: 0
Views: 5153
Reputation: 7860
In looking at the stack trace, it was definitely related to the ability to deserialize the XML. The tutorial that you're following is from 2009, and it appears that RestSharp has changed a bit out from under it.
I was able to get your sample to work, and access the list of plants using 'plants.Data`, with one small change:
var plants = client.Execute<PowerPlantsDTO>(request);
becomes
var plants = client.Execute<List<item>>(request);
In looking through the RestSharp code, it seems that they're using the name of the generic argument within the XmlDeserializer in order to determine the XML node name. Out of curiosity, I took a quick look at using the XmlElement attribute to try and influence the name of XML node, but the attribute cannot be applied to classes. As far as I am aware, your best bet will be to shape your DTO to explicitly match the expected return structure.
Hope that helps.
Upvotes: 1