RestSharp邮政JSON对象 [英] RestSharp Post a JSON Object
问题描述
我想张贴以下JSON与RestSharp:
I am trying to post the following JSON with RestSharp:
{"UserName":"UAT1206252627",
"SecurityQuestion":{
"Id":"Q03",
"Answer":"Business",
"Hint":"The answer is Business"
},
}
我认为我接近,但我似乎与挣扎的< STRONG> SecurityQuestion (API被抛出一个错误说一个参数丢失,但它并没有说是哪一个)
I think that I am close, but I seem to be struggling with the SecurityQuestion (the API is throwing an error saying a parameter is missing, but it doesn't say which one)
这是我的代码到目前为止:
This is the code I have so far:
var request = new RestRequest("api/register", Method.POST);
request.RequestFormat = DataFormat.Json;
request.AddParameter("UserName", "UAT1206252627");
SecurityQuestion securityQuestion = new SecurityQuestion("Q03");
request.AddParameter("SecurityQuestion", request.JsonSerializer.Serialize(securityQuestion));
IRestResponse response = client.Execute(request);
和我的安全问题类看起来是这样的:
And my Security Question class looks like this:
public class SecurityQuestion
{
public string id {get; set;}
public string answer {get; set;}
public string hint {get; set;}
public SecurityQuestion(string id)
{
this.id = id;
answer = "Business";
hint = "The answer is Business";
}
}
谁能告诉我什么,我做错了什么?是否有任何其他方式张贴的安全问题的对象?
Can anyone tell me what I am doing wrong? Is there any other way to post the Security Question object ?
非常感谢。
推荐答案
您需要指定内容类型的标题:
You need to specify the content-type in the header:
request.AddHeader("Content-type", "application/json");
此外 AddParameter
添加到信息或网址查询字符串基于法
Also AddParameter
adds to POST or URL querystring based on Method
我认为你需要将它添加到身体是这样的:
I think you need to add it to the body like this:
request.AddJsonBody(
new
{
UserName = "UAT1206252627",
SecurityQuestion = securityQuestion
}); // AddJsonBody serializes the object automatically
这篇关于RestSharp邮政JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!