REST的响应(通过Powershell的POST)未完成



我开始使用Powershell的Invoke-RestMethod,现在我遇到了一个问题,即我在POST后对监控工具的REST API的响应不完整。如果我正在使用其他工具,比如浏览器的REST扩展,我会得到更多的反馈:

响应REST扩展浏览器:

{
"entry": [
{
"@datatype": "int",
"@name": "CheckTime",
"value": {
"@type": "xs:int",
"$": "1542802849"
}
},
{
"@datatype": "int",
"@name": "AvailPageFile",
"value": {
"@type": "xs:int",
"$": "12345"
}
}
]
}

响应Powershell调用RestMethod:

entry
-----
@{@datatype=int; @name=CheckTime; value=} @{@datatype=int; @name=AvailPageFile; value=}

因此缺少Value=之后的{}的第二级。

这是我的代码:

$username = "user"
$password = "password"
$url = "http://urlREST"
$headers = @{
"Authorization" = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$($username):$($password)")); 
"Accept" = 'application/json ';
}

$data = @{}
$body = ConvertTo-Json $data
Invoke-RestMethod -Uri $url -Headers $headers -ContentType "application/json" -Method Post -Body $body 

有人给我小费吗?

感谢

您基本上得到了相同的响应,但它在PS对象内部。尝试以下操作:

$response = Invoke-RestMethod -Uri $url -Headers $headers -ContentType "application/json" -Method Post -Body $body

现在使用$response参数,例如:

$response.entry
$response.entry.name

等等。也做一个:

$response.gettype()

了解有关PowerShell对象的详细信息。如果这仍然不能给你正确的信息,请搜索responseStream以及如何获得它!

最新更新