为什么我在 Rest Client 中测试 Response payload 后总是收到 400 malformed?

Why do I keep getting 400 malformed when I have tested the Response payload in Rest Client?

它类似于我之前问过的一个问题,但在这种情况下,即使我已经在 Rest Client 上测试了响应负载,我仍然收到 400 malformed;下面的 link 可以将您定向到带有数据的图像。

1) https://drive.google.com/file/d/1gJ_och30jQTrcT36RvIbQSmxtqu0zJdD/view?usp=sharing

2) https://drive.google.com/file/d/1uZho4-73NRs4gGtXph25nRxUbyH-eq-f/view

输出:

Invoke-RestMethod : 400 MalformedCONTENTThe data request is malformed. Required content is missing or empty.Could not acquire data.
At C:\Users\sams\Documents\Reader_Test tRY hMM.ps1:29 char:3
+   Invoke-RestMethod -Method PUT -Uri $url -Headers $headers3 -Body $b ...
+   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

当前代码:

$url = "http://##.###.###.#:####/reader/blink-led"

$headers3 = @{
    "Host"="##.###.###.#:####";
    "Authorization"="Basic dhgageyngixjsklxsfnhjopughNzk5fkswpi"
}     

$body = @'
  {
   "payload": {
          "sensorId": "se:eh:fu:co:c7",
          "blinkCount": 5,
          "blinkOn": 200,
          "blinkOff": 200,
          "countPause": 2,
          "timeout": 5000
      }
  }
  '@

  Invoke-RestMethod -Method PUT -Uri $url -Headers $headers3 -Body $body -ContentType "application/json"

为什么说必填内容缺失或为空?我已经在 Rest Client 上测试了 Request 有效负载并且它有效。 任何帮助表示赞赏。

假设有效负载是正确的,请尝试以下操作(一次使用 -ContentType "application/json",一次不使用)。

$uri = "http://##.###.###.#:####/reader/blink-led";

$headers = @{
    Host = "##.###.###.#:####";
    Authorization = "Basic dhgageyngixjsklxsfnhjopughNzk5fkswpi"
};

$body = @{ payload = @{ sensorId = 'se:eh:fu:co:c7'; blinkCount = 5; blinkOn = 200; blinkOff = 200; countPause = 2; timeout = 5000 }} | ConvertTo-Json;

Invoke-RestMethod -Method PUT -Uri $uri -Headers $headers -Body $body -ContentType "application/json";

JSON 部分有效,但您使用的 here-string 部分无效。 您应该删除最后一个 '@ 之前的两个 space 字符: 如果您在 ISE 编辑器中查看它,您会看到一条红色的波浪线,如果您将鼠标悬停在它上面,您会看到:

$body = @'
  {
   "payload": {
          "sensorId": "se:eh:fu:co:c7",
          "blinkCount": 5,
          "blinkOn": 200,
          "blinkOff": 200,
          "countPause": 2,
          "timeout": 5000
      }
  }
'@