它类似于我之前提出的一个问题,但在这种情况下,即使我在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有效负载,但它确实有效。任何帮助表示赞赏。
假设有效负载是正确的,请尝试以下(一次使用-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";