c# - 如何使用 Restsharp 添加 bool 参数来解析 REST 请求?

标签 c# rest parsing restsharp

我使用nodechef Parse作为后端服务器。我有一个这样的对象:

 public class UserJson
    {
        ...
        public string myEmail { get; set; }
        public bool needsEmail { get; set; }
    }

我使用 REST 与后端服务器进行通信,并使用 Restsharp 来构建我的请求。我想更新“myEmail”字段,它是字符串。我这样写:

RestRequest request = new RestRequest();
request.Method = Method.PUT;
request.AddParameter("myEmail", "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="721f0b151f131b1e32151f131b1e5c111d1f" rel="noreferrer noopener nofollow">[email protected]</a>");

我的请求顺利,我得到了良好的响应。 但如果想更新像“needsEmail”这样的 bool 字段,我会这样写:

request.AddParameter("needsEmail", false);

我从后端服务器 Parse 收到了 BadRequest 响应。我的问题是: 如何使用 Restsharp 更新 bool 值

最佳答案

使用...

request.AddParameter("myEmail", "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="365b4f515b575f5a76515b575f5a1855595b" rel="noreferrer noopener nofollow">[email protected]</a>");
request.AddParameter("needsEmail", false);

将其发送到服务器...

{ "myEmail" : "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="731e0a141e121a1f33141e121a1f5d101c1e" rel="noreferrer noopener nofollow">[email protected]</a>", "needsEmail": "false" }

如果以字符串形式发送 false 不是服务器想要/期望的,您可以通过这种方式添加参数..

var body = new { myEmail = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="523f2b353f333b3e12353f333b3e7c313d3f" rel="noreferrer noopener nofollow">[email protected]</a>", needsEmail = false };
request.AddJsonBody(body);

这将发送...

{ "myEmail" : "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dbb6a2bcb6bab2b79bbcb6bab2b7f5b8b4b6" rel="noreferrer noopener nofollow">[email protected]</a>", "needsEmail": false }

这可能会让服务器对您的情况感到满意。

关于c# - 如何使用 Restsharp 添加 bool 参数来解析 REST 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41040476/

相关文章:

c# - 接收并发异步请求并一次处理一个请求

c# - 将字符串的每个部分在 c# 中的另一个字符串中的第一次出现作为子字符串

java - Jersey 2.2 : ContainerResponseFilter and ContainerRequestFilter never get executed

parsing - Bison 减少/减少与强制转换和表达式括号的冲突

c# - AsyncPostBackTrigger 不是已知元素?

iphone - iphone中的休息网络服务

python - 如何在 flask 中发布请求后使用 websocket 向客户端发送警报

java - 如何解析文件

c++ - boost::未排序模式的精神语法

c# - MySqlContext如何将查询写入变量