13

I'm trying to do a very simple example of using RestSharp's Execute method of querying a rest endpoint and serializing to a POCO. However, everything I try results in a response.Data object that has all properties with a NULL value.

Here is the JSON response:

{
   "Result":
   {
       "Location":
       {
           "BusinessUnit": "BTA",
           "BusinessUnitName": "CASINO",
           "LocationId": "4070",
           "LocationCode": "ZBTA",
           "LocationName": "Name of Casino"
       }
   }
}

Here is my test code

 [TestMethod]
    public void TestLocationsGetById()
    {
        //given
        var request = new RestRequest();
        request.Resource = serviceEndpoint + "/{singleItemTestId}";
        request.Method = Method.GET;
        request.AddHeader("accept", Configuration.JSONContentType);
        request.RootElement = "Location";
        request.AddParameter("singleItemTestId", singleItemTestId, ParameterType.UrlSegment);
        request.RequestFormat = DataFormat.Json;

        //when
        Location location = api.Execute<Location>(request);            

        //then
        Assert.IsNotNull(location.LocationId); //fails - all properties are returned null

    }

And here is my API code

 public T Execute<T>(RestRequest request) where T : new()
    {
        var client = new RestClient();
        client.BaseUrl = Configuration.ESBRestBaseURL;

        //request.OnBeforeDeserialization = resp => { resp.ContentLength = 761; };

        var response = client.Execute<T>(request);
        return response.Data;
    }

And finally, here is my POCO

 public class Location
{        
    public string BusinessUnit { get; set; }
    public string BusinessUnitName { get; set; }
    public string LocationId { get; set; }
    public string LocationCode { get; set; }
    public string LocationName { get; set; }
}

Additionally, the ErrorException and ErrorResponse properties on the response are NULL.

This seems like a very simple case, but I've been running around in circles all day! Thanks.

smercer
  • 916
  • 1
  • 6
  • 15
  • What happens when you call `request.AddUrlSegment("singleItemTestId", singleItemTestId)` instead of the call you have to `AddParameter`? – David Hoerster Jun 18 '12 at 17:17

1 Answers1

10

What is the Content-Type in the response? If not a standard content type like "application/json", etc. then RestSharp won't understand which deserializer to use. If it is in fact a content type not "understood" by RestSharp (you can verify by inspecting the Accept sent in the request), then you can solve this by doing:

client.AddHandler("my_custom_type", new JsonDeserializer());

EDIT:

Ok, sorry, looking at the JSON again, you need something like:

public class LocationResponse
   public LocationResult Result { get; set; }
}

public class LocationResult {
  public Location Location { get; set; }
}

And then do:

client.Execute<LocationResponse>(request);
Pete
  • 11,313
  • 4
  • 43
  • 54
  • The content-type is "application/json". Shouldn't this line: request.RootElement = "Location"; remove the need for the "LocationResponse" object wrapper that you suggested? – smercer Jun 18 '12 at 17:24
  • Hmmm, after I tried what your second edit recommended it worked, but I'm not sure I understand why, unless I am completely misunderstanding the purpose of the RootElement property. Well, THANKS! – smercer Jun 18 '12 at 17:32
  • 3
    The `RootElement` support in JsonDeserializer only seems to support specifying a property on the toplevel object as the element, e.g. "Result" in your JSON. It doesn't do searching deeper into the object hierarchy: https://github.com/restsharp/RestSharp/blob/master/RestSharp/Deserializers/JsonDeserializer.cs – Pete Jun 18 '12 at 17:51