尝试让restsharp匹配来自邮递员的curl代码

发布于 2025-01-11 16:22:02 字数 1782 浏览 0 评论 0原文

这个问题已经被问过一百次了,但似乎没有一个对我有用。我有以下curl代码:

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时不同。这是我的rest Sharp代码:

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代码通过postman运行它时,服务器接受请求,但是当我通过restsharp运行它时,我得到了错误的请求错误。

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

This has been asked a hundred times, but none of them seem to work for me. I have the following curl code:

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
            }
        ]
    }
]'

When I run this in postman I get a different response from the server than when I try to work it into restsharp. Here is my rest sharp code:

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

The value of the variable datapoint is:

[
   {
      "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
         }
      ]
   }
]

When I run it through postman with the curl code the server accepts the request, but when I run it through restsharp I get bad request error.

EDITED the code to match the below comment, as doing so didn't solve my problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

兔姬 2025-01-18 16:22:02

我非常确定您使用 AddParameter 定义的内容类型是罪魁祸首。您是否像这样测试过:

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

或使用 AddJsonBody

request.AddJsonBody(datapoint);

I'm pretty sure the content type you define using the AddParameter is the culprit. Have you tested it like so:

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

or using AddJsonBody

request.AddJsonBody(datapoint);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文