在Rest Client中测试响应有效负载后,为什么我仍然收到400格式错误的消息?

时间:2019-02-13 04:41:16

标签: rest powershell put

这与我之前提出的问题类似,但是在这种情况下,即使我已经在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有效负载,并且可以正常工作。 任何帮助表示赞赏。

2 个答案:

答案 0 :(得分: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";

答案 1 :(得分: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
      }
  }
'@