【问题标题】:RestSharp Post a JSON ObjectRestSharp 发布一个 JSON 对象
【发布时间】:2018-04-06 10:01:21
【问题描述】:

我正在尝试使用 RestSharp 发布以下 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);

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

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

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

非常感谢。

【问题讨论】:

    标签: c# json restsharp


    【解决方案1】:

    您需要在标头中指定内容类型:

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

    AddParameter 基于方法添加到 POST 或 URL 查询字符串

    我认为你需要像这样将它添加到正文中:

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

    【讨论】:

    • 感谢您的回答 Oluwafemi,但我仍然收到相同的错误(缺少参数) - 我知道这似乎应该可行,但我还有什么可以尝试的吗?跨度>
    • 能否给出APl的示例方法?
    • 它对我有用:request.AddHeader("Content-Type", "application/json; charset=utf-8"); request.AddJsonBody(yourobject);
    • 感谢您的解决方案
    • 真的不行。正确的做法是:r.AddParameter("application/json", JsonSerializerHelper.Serialize(keys), ParameterType.RequestBody);
    【解决方案2】:

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

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

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

    它成功了!

    【讨论】:

      【解决方案3】:

      要发布原始 json 正文字符串,AddBody() 或 AddJsonBody() 方法将不起作用。请改用以下内容

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

      【讨论】:

      • 是的,原始方法很多时候很有用,例如,当传入存储在数据库中的主体时
      【解决方案4】:

      看起来最简单的方法是让 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;
          }
      

      【讨论】:

        【解决方案5】:

        通过AddObject方法从对象支持RestSharp

        request.AddObject(securityQuestion);
        

        【讨论】:

          猜你喜欢
          • 2019-12-06
          • 1970-01-01
          • 2014-08-25
          • 2011-09-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-10-20
          • 1970-01-01
          相关资源
          最近更新 更多