如何使用Restsharp向Parse REST请求添加布尔参数?

时间:2016-12-08 13:18:26

标签: c# rest parsing restsharp

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

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

我使用REST与后端服务器进行通信,使用R​​estsharp构建我的请求。我想更新“myEmail”字段,这是字符串。我写这个:

RestRequest request = new RestRequest();
request.Method = Method.PUT;
request.AddParameter("myEmail", "mygmail@gmail.com");

我的请求没问题,我得到了一个好的回复。 但是如果想要更新像“needsEmail”这样的布尔字段,我写这个:

request.AddParameter("needsEmail", false);

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

更新布尔值

1 个答案:

答案 0 :(得分:1)

使用...

request.AddParameter("myEmail", "mygmail@gmail.com");
request.AddParameter("needsEmail", false);

将此内容发送到服务器......

{ "myEmail" : "mygmail@gmail.com", "needsEmail": "false" }

如果作为字符串发送false不是服务器想要/期望的,那么您可以这样添加参数..

var body = new { myEmail = "mygmail@gmail.com", needsEmail = false };
request.AddJsonBody(body);

将发送......

{ "myEmail" : "mygmail@gmail.com", "needsEmail": false }

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