6

When I make a request in RestSharp like so:

var response = client.Execute<bool>(request);

I get the following error:

"Unable to cast object of type 'System.Boolean' to type 'System.Collections.Generic.IDictionary`2[System.String,System.Object]'."

This is complete HTTP response, per Fiddler:

HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Mon, 01 Apr 2013 15:09:14 GMT
Content-Length: 5

false

It appears that everything is kosher with the response, so what gives?

Also, if I'm doing something stupid with my WebAPI Controller by returning a simple value instead of an object and that would fix my problem, feel free to suggest.

Josh Kodroff
  • 27,301
  • 27
  • 95
  • 148

1 Answers1

9

RestSharp will only deserialise valid json. false is not valid json (according to RFC-4627). The server will need to return something like the following at the least:

{ "foo": false }

And you'll need a class like to following to deserialize to:

public class BooleanResponse
{
    public bool Foo { get; set; }
}
Andrew Young
  • 1,779
  • 1
  • 13
  • 27
  • Oh. Right. That makes sense. My bad. – Josh Kodroff Apr 01 '13 at 18:31
  • 1
    FYI, that's exactly what you're supposed to return "Json(true)" from a RemoteValidationAttribute. Surprised that MSFT has us return invalid JSON in their own example. See: http://msdn.microsoft.com/en-us/library/gg508808(v=vs.98).aspx – Josh Kodroff Jan 03 '14 at 17:30
  • Slightly incorrect. `false` *is* actually valid JSON but I don't think it's valid to return it alone as `application/json` . See http://stackoverflow.com/questions/19569221/did-the-publication-of-ecma-404-affect-the-validity-of-json-texts-such-as-2-or and http://stackoverflow.com/questions/18419428/what-is-the-minimum-valid-json – theyetiman Oct 08 '15 at 15:01