当我在Rest Client中测试了Response有效负载时,为什么我会一直遇到400格式错误?

问题描述 投票:0回答:2

它类似于我之前提出的一个问题,但在这种情况下,即使我在Rest Client上测试了响应有效负载,我仍然会遇到400错误信息。下面的链接可以引导您到带有数据的图像。

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\2 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有效负载,但它确实有效。任何帮助表示赞赏。

rest powershell put
2个回答
0
投票

假设有效负载是正确的,请尝试以下(一次使用-Co​​ntentType“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";

0
投票

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

enter image description here

$body = @'
  {
   "payload": {
          "sensorId": "se:eh:fu:co:c7",
          "blinkCount": 5,
          "blinkOn": 200,
          "blinkOff": 200,
          "countPause": 2,
          "timeout": 5000
      }
  }
'@
© www.soinside.com 2019 - 2024. All rights reserved.