试图让 restsharp 匹配邮递员的卷曲代码

Trying to get restsharp to match curl code from postman

这已被问过一百次,但其中 none 似乎对我有用。我有以下卷曲代码:

curl --location --request POST 'https://endpoint/telemetry' \
--header 'x-ads-dev: akeyvalue' \
--header 'Content-Type: application/json' \
--data-raw '[
    {
        "entityID": 2123,
        "locationID": 33,
        "dataPoints": [
            {
                "dateTime": "2020-08-03 23:05:00",
                "reading": 0,
                "flags": 0,
                "quality": 0,
                "ignore": true
            },
            {
                "dateTime": "2020-09-27 03:10:00",
                "reading": 0,
                "flags": 0,
                "quality": 0,
                "ignore": true
            }
        ]
    }
]'

当我在邮递员中 运行 时,我从服务器得到的响应与我尝试将其处理到 restsharp 时不同。这是我的休息时间代码:

try {
    string url = "https://endpoint/";
    var client = new RestClient(url);
    client.AddDefaultHeader("x-ads-dev", "akeyvalue");
    var request = new RestRequest("telemetry", Method.Post);
    request.AddHeader("Content-Type", "application/json");
    request.AddParameter("application/json", datapoint, ParameterType.RequestBody);
    var response =  await client.PostAsync(request);

变量数据点的值为:

[
   {
      "entityID":2123,
      "locationID":33,
      "dataPoints":[
         {
            "dateTime":"2020-08-03 23:05:00",
            "reading":0,
            "flags":0,
            "quality":0,
            "ignore":true
         },
         {
            "dateTime":"2020-09-27 03:10:00",
            "reading":0,
            "flags":0,
            "quality":0,
            "ignore":true
         }
      ]
   }
]

当我 运行 通过邮递员使用 curl 代码时,服务器接受了请求,但是当我 运行 通过 restsharp 时,我得到了错误的请求错误。

编辑代码以匹配以下评论,因为这样做并没有解决我的问题。

我很确定您使用 AddParameter 定义的内容类型是罪魁祸首。你有没有这样测试过:

request.AddParameter("application/json", datapoint, ParameterType.RequestBody);

或使用AddJsonBody

request.AddJsonBody(datapoint);