简体   繁体   English

Powershell Invoke-RestMethod内部服务器错误

[英]Powershell Invoke-RestMethod Internal Server Error

When I run this (parameters and body that worked from Postman): 当我运行此命令时(Postman工作的参数和正文):

$Url = "http://${IPADDR}:8080/api/v1/topology/query"

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add('Access-Token', 'token')
$headers.Add('Content-Type', 'application/json')
$headers.Add('Accept', 'application/json')

$json = 
'{
"includes":[{"ids":["264690t5te74hy4y"],"observationName":"page_life_expectancy"}],
"startTime":1528718400000,
"endTime":1528768800000,
"granularity":3600000,
"numberOfValue":1,
"retrievalType":"RAW"
}'

$response = Invoke-RestMethod -Method 'Post' -Uri $url -Headers $headers -Body $json

$ple = $response | select -ExpandProperty data | select max

in Powershell ISE, I get this: 在Powershell ISE中,我得到以下信息:

An error occurred while calling REST method at: http://${IPADDR}:8080/api/v1/topology/query. 在以下位置调用REST方法时发生错误:http:// $ {IPADDR}:8080 / api / v1 / topology / query。 Error: The remote server returned an error: (500) Internal Server Error.. Response body: Apache Tomcat/7.0.82 - Error report 错误:远程服务器返回错误:(500)内部服务器错误。响应正文:Apache Tomcat / 7.0.82-错误报告

Any expert in Powershell, JSON, and REST API that can help me with this issue? Powershell,JSON和REST API中的任何专家都可以帮助解决此问题吗?

The content of the Body parameter of Invoke-RestMethod should be an object serialized in JSon. Invoke-RestMethod的Body参数的内容应该是在JSon中序列化的对象。 In your example, you have 3 levels of serialization. 在您的示例中,您有3个级别的序列化。

You should remove 2 levels of serialization: 您应该删除2个级别的序列化:

$jsonBody = '{
"includes":[{"ids": 
    ["264690t5te74hy4y"],"observationName":"page_life_expectancy"}],
    "startTime":1528718400000,
    "endTime":1528768800000,
    "granularity":3600000,
    "numberOfValue":1,
   "retrievalType":"RAW"
}'

$response = Invoke-RestMethod -Method 'Post' -Uri $url -Headers $headers -Body $jsonBody

But it's not guaranteed that the error 500 disappear. 但是并不能保证错误500消失。

You may have more details about the error with the Exception content. 您可能会通过“异常”内容获得有关该错误的更多详细信息。 You can try that: 您可以尝试:

try {
    $response = Invoke-RestMethod -Method 'Post' -Uri $url -Headers $headers -Body $jsonBody
}
catch {
    $errorMessage = $_.Exception.Message
    if (Get-Member -InputObject $_.Exception -Name 'Response') {
        try {
            $result = $_.Exception.Response.GetResponseStream()
            $reader = New-Object System.IO.StreamReader($result)
            $reader.BaseStream.Position = 0
            $reader.DiscardBufferedData()
            $responseBody = $reader.ReadToEnd();
        } catch {
            Throw "An error occurred while calling REST method at: $url. Error: $errorMessage. Cannot get more information."
        }
    }
    Throw "An error occurred while calling REST method at: $url. Error: $errorMessage. Response body: $responseBody"
}

Error handling from post: How to get Powershell Invoke-Restmethod to return body of http 500 code response 帖子中的错误处理: 如何获取Powershell Invoke-Restmethod返回HTTP 500代码响应的主体

From PowerShell since you serialize the content to JSON, specify -ContentType "application/json". 由于将内容序列化为JSON,因此在PowerShell中,请指定-ContentType“ application / json”。 Also, if you think the content might contain unicode strings, include the -ContentType "application/json; charset=utf-8". 另外,如果您认为内容可能包含unicode字符串,请包含-ContentType“ application / json; charset = utf-8”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM