我希望你一切都好!
我需要使用restsharp进行发布请求,要添加到请求中的正文如下所示。这比我之前完成的发布请求要复杂一些。
我需要协助的复杂帖子: { “ contextInfoId”:“字符串”, “ userId”:“字符串”, “ specificOs”:“字符串”, “ buildPlatform”:“ string”, “ deviceName”:“ string”, “ deviceId”:“字符串”, “ token”:“ string”, “ loginInfo”:{ “ loginInfoId”:“字符串”, “ loginDate”:“ 2019-03-14T06:21:39.693Z” }
我使用以下代码添加了要请求的基本帖子正文:
//添加json数据/正文 request.AddJsonBody(new {buProfileId =“ 1”,divisionNames =“ IDC”,businessUnitNames =“ XYZ”,processGroupNames =“ ABC”,systemOrProjectName =“ Test”,customername =“ User”});
上面的C#代码对类似下面这样的主体很有效。
{ “ buProfileId”:“字符串”, “ divisionNames”:“ string”, “ businessUnitNames”:“字符串”, “ processGroupNames”:“字符串”, “ systemOrProjectName”:“字符串”, “ customername”:“字符串” }
请让我知道如何完成复杂的后期操作。
答案 0 :(得分:1)
您可以创建一个json对象并为其分配值,然后可以序列化json并发送正文
public class LoginInfo
{
public string loginInfoId { get; set; }
public DateTime loginDate { get; set; }
}
public class Context
{
public string contextInfoId { get; set; }
public string userId { get; set; }
public string specificOs { get; set; }
public string buildPlatform { get; set; }
public string deviceName { get; set; }
public string deviceId { get; set; }
public string token { get; set; }
public LoginInfo loginInfo { get; set; }
}
public IRestResponse Post_New_RequestType(string context, string user_ID, string Specific_Os, string Build_Platfrom, string Device_Name, string device_Id, string Token_Value, string login_infoId, DateTime Login_Date)
{
Context tmp = new Context();
tmp.contextInfoId = context;
tmp.userId = user_ID;
tmp.specificOs = Specific_Os;
tmp.buildPlatform = Build_Platfrom;
tmp.deviceName = Device_Name;
tmp.deviceId = device_Id;
tmp.token = Token_Value;
tmp.loginInfo.loginInfoId = login_infoId;
tmp.loginInfo.loginDate = Login_Date;
string json = JsonConvert.SerializeObject(tmp);
var Client = new RestClient(HostUrl);
var request = new RestRequest(Method.POST);
request.Resource = string.Format("/api/example");
request.AddParameter("application/json", json, ParameterType.RequestBody);
IRestResponse response = Client.Execute(request);
return response;
}