【问题标题】:Empty value returned by RestSharpRestSharp 返回的空值
【发布时间】:2022-11-16 21:42:53
【问题描述】:

下面的代码块返回一个空值。但是当我在邮递员端发送请求时,数据毫无问题地到达了我。你认为问题是什么?你能帮忙吗?谢谢你。

代码:

ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

var client = new RestClient("url");
client.Timeout = -1;
var request = new RestRequest(Method.GET);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Authorization", "Basic mytoken");
var body = @"{" + "\n" +
@"    ""size"": 1," + "\n" +
@"    ""page"": 1," + "\n" +
@"    ""start_date"": ""2022-01-02""," + "\n" +
@"    ""end_date"": ""2022-01-03""" + "\n" +
@"}";
request.AddParameter("application/json", body, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
MessageBox.Show(response.Content);

REST 返回空值

【问题讨论】:

    标签: c# .net api rest restsharp


    【解决方案1】:

    尝试使用 AddJsonBody(),而不是 AddParameter() 匿名对象

    //Kindly check the type of start_date and end_date property.
    var body = new { Size = 1, Page = 1, start_date="2022-01-02", end_date="2022-01-03"};
    request.AddJsonBody(body);
    

    From Documentation:

    AddParameter("application/json", ..., ParameterType.RequestBody)不会工作,改用AddBody(),或者更好,AddJsonBody

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-09
      • 2012-11-13
      • 1970-01-01
      • 1970-01-01
      • 2021-07-07
      相关资源
      最近更新 更多