如何在RestSharp中发布许多不同的字段名称?

时间:2019-12-17 09:47:34

标签: c# rest api post restsharp

如何在RestSharp中发布许多不同的字段名称? 下面是我尝试过的代码,就像这样或其他方式。 这里的任何帮助将不胜感激。

JObject jsonPOST = new JObject();

jsonPOST.Add("VariableName1", "temp1" );

jsonPOST.Add("VariableName2", "temp2" );



JObject jsonPOST1 = new JObject();

jsonPOST1.Add("VariableName1", "temp1" );

jsonPOST1.Add("VariableName2", "temp2" );


JObject jsonPOST2 = new JObject();

jsonPOST1.Add("number1", "2" );

jsonPOST1.Add("number2", "4" );

restRequest.AddParameter("application/json", jsonPOST , ParameterType.RequestBody);
restRequest.AddParameter("application/json", jsonPOST1 , ParameterType.RequestBody);
restRequest.AddParameter("application/json", jsonPOST2 , ParameterType.RequestBody);

如何使用上述RestSharp结构发布此类数据?

我希望这种格式的POST请求将发送到REST Api。

"{

"FieldName1":{

"VariableName1": "temp1",

"VariableName2": "temp2",

},

"FieldName2":{

"VariableName1": "temp1",

"VariableName2": "temp2",

},

"FieldName3": {

"number1": "2",

"number2": "4",

}

}"

1 个答案:

答案 0 :(得分:1)

您需要将所有usb_cam合并到一个JObject上。

JObject

然后添加到正文(转换为字符串)。 (我不知道设置主体的正确方法)

// Main JObject
var mainObj = new JObject();

// Syntax i love
var obj1 = new JObject
{
    {"name", "CorrM"},
    {"ip", "127.0.0.1"}
};

// Syntax are accepted too
var obj2 = new JObject();
obj2.Add("bla1", "bla");
obj2.Add("bla2", "bla");

// Combine on one JObject
mainObj.Add("FieldName1", obj1);
mainObj.Add("FieldName2", obj2);

输出字符串必须像这样

restRequest.AddParameter("application/json", mainObj.ToString(Formatting.None), ParameterType.RequestBody);