RestSharp AddBody在JSON参数中添加双引号

时间:2019-02-15 05:56:35

标签: c# json web-services restsharp

我正在使用RESTSharp调用API

  var client = new RestClient("http://demoservice.com");
  var request = new RestRequest("callapi", "put");          
  request.RequestFormat = DataFormat.Json;

string jsonaction = "{\"tokenid\":\"x123x45\",\"userid\":\"2456\",\"ip\":\"192.168.1.20\",\"transaction\":\"6\",\"actionCode\":\"78\",\"jtoken\":\"systemtoken\"}";
request.AddBody(new { action = "SAVE", data = "savedata", token = "systemtoken", jsonaction = jsonaction });

我正在检查传入请求的调试数据。我的预期输出如下

{"action":"SAVE","data":"savedata","token":"systemtoken","jsonaction":{"tokenid":"x123x45","userid":"2456","ip":"192.168.1.20","transaction":"6","actionCode":"78","jtoken":"systemtoken"}}

但是得到

{"action":"SAVE","data":"savedata","token":"systemtoken","jsonaction":"{"tokenid":"x123x45","userid":"2456","ip":"192.168.1.20","transaction":"6","actionCode":"78","jtoken":"systemtoken"}"}

如果有人可以指导如何发布JSON,我尝试使用Addbody和AddJsonBody,但没有任何效果。

3 个答案:

答案 0 :(得分:2)

  

您可以使用request.AddParameter()方法执行此操作:

request.Method = Method.POST;
request.AddHeader("Accept", "application/json");
request.Parameters.Clear();
request.AddParameter("application/json", data , ParameterType.RequestBody);

var response = client.Execute(request);
var content = response.Content; // raw content as string  

数据的格式为:

data :

{
  "action":"dosomething" ,
  "data":"somedata" ,
  "token":"sometoken",
  "jsonAction": {
    "tokenId": "",
    ...
}

希望有帮助!

答案 1 :(得分:1)

我建议您使用JObject来创建请求的正文,

JObject jObject = new JObject();
jObject["action"] = "SAVE";
jObject["data"] = "savedata";
jObject["token"] = "systemtoken";
jObject["jsonaction"] = JObject.Parse("{\"tokenid\":\"x123x45\",\"userid\":\"2456\",\"ip\":\"192.168.1.20\",\"transaction\":\"6\",\"actionCode\":\"78\",\"jtoken\":\"systemtoken\"}");

然后将此jObject传递给其中一个

request.AddBody(jObject.ToString());

OR

request.AddJsonBody(jObject.ToString());

对于JObject,您需要将using Newtonsoft.Json.Linq;命名空间导入程序,然后可以在newtonsoft.json包中找到该命名空间。

您甚至可以这样使用

request.AddBody(new { action = "SAVE", data = "savedata", token = "systemtoken", jsonaction = JObject.Parse(jsonaction) });

但是为您的完整json数据创建JObject最好是在使用字符串

创建自己的json数据时最大程度地减少错误和异常

输出:

enter image description here

答案 2 :(得分:0)

从两个天才的人那里,我可以完成以下代码

jsonaction objjsonaction = new jsonaction();
            objjsonaction.tokenid = "x123x45";
            objjsonaction.userid = "2456";
            objjsonaction.ip = "192.168.1.20";
            objjsonaction.transaction = "6";
            objjsonaction.actionCode = "78";
            objjsonaction.jtoken = gentoken("key"); // gentoken() is external function for generating token from key

string sobjjsonaction = Newtonsoft.Json.JsonConvert.SerializeObject(objjsonaction);
            sobjjsonaction = sobjjsonaction.Replace("__", "-");

            JObject jObject = new JObject();
            jObject["action"] = "SAVE";
            jObject["data"] = getpostdata(); // a function to generate data from db
            jObject["token"] = gentoken("key"); // gentoken() is external function for generating token from key
            jObject["jsonaction"] = JObject.Parse(sobjjsonaction);

            string sObject = Regex.Replace(jObject.ToString(), @"\s+", "");

            //request.Method = Method.PUT;
            request.AddHeader("Accept", "application/json");
            request.Parameters.Clear();
            request.AddParameter("application/json", sObject, ParameterType.RequestBody);