Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does RestSharp throw an error when deserializing a boolean response?

Tags:

restsharp

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.

like image 608
Josh Kodroff Avatar asked Apr 01 '13 15:04

Josh Kodroff


1 Answers

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; }
}
like image 117
Andrew Young Avatar answered Oct 23 '22 02:10

Andrew Young