这与我之前提出的问题类似,但是在这种情况下,即使我已经在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有效负载,并且可以正常工作。 任何帮助表示赞赏。
答案 0 :(得分:0)
假设有效载荷正确,请尝试以下操作(一次使用-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";
答案 1 :(得分:0)