RestSharp Post 一个 JSON 对象

RestSharp Post a JSON Object

我正在尝试使用 RestSharp post 以下 JSON:

{"UserName":"UAT1206252627",
"SecurityQuestion":{
    "Id":"Q03",
    "Answer":"Business",
    "Hint":"The answer is Business"
},
}

我认为我很接近,但我似乎正在努力解决 SecurityQuestion(API 抛出一个错误,说缺少一个参数,但它没说是哪一个)

这是我目前的代码:

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);

我的安全问题 class 如下所示:

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";
    }
}

谁能告诉我我做错了什么?有没有其他方法可以 post 安全问题对象?

非常感谢。

您需要在 header 中指定 content-type:

request.AddHeader("Content-type", "application/json");

另外 AddParameter 添加到 POST 或 URL 基于方法

的查询字符串

我认为您需要像这样将其添加到 body:

request.AddJsonBody(
    new 
    {
      UserName = "UAT1206252627", 
      SecurityQuestion = securityQuestion
    }); // AddJsonBody serializes the object automatically

再次感谢您的帮助。为了让它工作,我必须将所有内容作为一个参数提交。这是我最后使用的代码。

首先我做了几个 类 请求对象和安全问题:

public class SecurityQuestion
{
    public string Id { get; set; }
    public string Answer { get; set; }
    public string Hint { get; set; }
}

public class RequestObject
{
    public string UserName { get; set; }
    public SecurityQuestion SecurityQuestion { get; set; }
}

然后我只是把它作为一个单独的参数添加,并在发布之前将它序列化为JSON,就像这样:

var yourobject = new RequestObject
            {
                UserName = "UAT1206252627",
                SecurityQuestion = new SecurityQuestion
                {
                    Id = "Q03",
                    Answer = "Business",
                    Hint = "The answer is Business"
                },
            };
var json = request.JsonSerializer.Serialize(yourobject);

request.AddParameter("application/json; charset=utf-8", json, ParameterType.RequestBody);

IRestResponse response = client.Execute(request);

成功了!

RestSharp 通过 AddObject 方法

对象支持
request.AddObject(securityQuestion);

To post 原始 json 正文字符串、AddBody() 或 AddJsonBody() 方法将不起作用。请改用以下内容

request.AddParameter(
   "application/json",
   "{ \"username\": \"johndoe\", \"password\": \"secretpassword\" }", // <- your JSON string
   ParameterType.RequestBody);

看起来最简单的方法是让 RestSharp 处理所有序列化。您只需要像这样指定 RequestFormat 即可。这是我为我正在做的事情想出的。 .

    public List<YourReturnType> Get(RestRequest request)
    {
        var request = new RestRequest
        {
            Resource = "YourResource",
            RequestFormat = DataFormat.Json,
            Method = Method.POST
        };
        request.AddBody(new YourRequestType());

        var response = Execute<List<YourReturnType>>(request);
        return response.Data;
    }

    public T Execute<T>(RestRequest request) where T : new()
    {
        var client = new RestClient(_baseUrl);

        var response = client.Execute<T>(request);
        return response.Data;
    }