gpt4 book ai didi

restsharp - 为什么 RestSharp 在反序列化 bool 响应时会抛出错误?

转载 作者:行者123 更新时间:2023-12-02 22:21:43 24 4
gpt4 key购买 nike

当我像这样在 RestSharp 中发出请求时:

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

我收到以下错误:

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

根据 Fiddler 的说法,这是完整的 HTTP 响应:

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

看起来一切都符合响应,那么是什么给出了呢?

另外,如果我用 WebAPI Controller 做了一些愚蠢的事情,通过返回一个简单的值而不是一个对象来解决我的问题,请随时提出建议。

最佳答案

RestSharp 只会反序列化有效的 json。 false 不是有效的 json(根据 RFC-4627)。服务器至少需要返回如下内容:

{ "foo": false }

您需要一个类似以下的类来反序列化:

public class BooleanResponse
{
public bool Foo { get; set; }
}

关于restsharp - 为什么 RestSharp 在反序列化 bool 响应时会抛出错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15746061/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com