【问题标题】:Powershell Invoke-RestMethod Internal Server ErrorPowershell Invoke-RestMethod 内部服务器错误
【发布时间】:2019-09-26 02:15:20
【问题描述】:

当我运行这个(从 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

在 Powershell ISE 中,我得到了这个:

在以下位置调用 REST 方法时出错:http://${IPADDR}:8080/api/v1/topology/query。错误:远程服务器返回一个 错误:(500)内部服务器错误..响应正文:Apache Tomcat/7.0.82 - 错误报告

有任何 Powershell、JSON 和 REST API 方面的专家可以帮助我解决这个问题吗?

【问题讨论】:

  • 你为什么要转换你的json?它没有任何作用。
  • 没有人可以帮助您解决“500 内部服务器错误”。使用不同的工具(例如Postman)并让请求手动工作。一旦你有了一些绝对有效的东西,你就可以在 Powershell 中轻松地重新创建它。

标签: powershell


【解决方案1】:

Invoke-RestMethod 的 Body 参数的内容应该是 JSon 中序列化的对象。在您的示例中,您有 3 级序列化。

您应该删除 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

但不保证错误500消失。

您可能对异常内容的错误有更多详细信息。你可以试试:

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"
}

来自帖子的错误处理:How to get Powershell Invoke-Restmethod to return body of http 500 code response

【讨论】:

  • 感谢 Julien 提供的有用见解!我像您一样更改了我的代码,字符串 $jsonbody 实际上是邮递员工作的正文,但是,我仍然收到此错误:远程服务器返回错误:(500)内部服务器错误。在 line:26 char:5 + Throw $_.Exception + ~~~~~~~~~~~~~~~~~~ + CategoryInfo : OperationStopped: (:) [], WebException + FullyQualifiedErrorId : 远程服务器返回错误:(500) 内部服务器错误。
  • 您是否尝试过 try {} catch {} 部分以获取有关错误的更多信息?你应该有这样的输出:StatusCode: 500, StatusDescription: xxx
  • 我确实运行了你的 try{} catch{} 行。我不确定为什么它没有返回像“StatusCode”这样的输出......有什么想法吗?
  • StatusCode: 和 StatusDescription: 应该出现在错误之前。您能否尝试删除行 Throw $_.Exception 以更好地查看输出?
  • 哦,是的,我的错。它说 StatusCode: 500 StatusDescription: Internal Server Error
【解决方案2】:

在将内容序列化为 JSON 后,从 PowerShell 中指定 -ContentType "application/json"。此外,如果您认为内容可能包含 unicode 字符串,请包括 -ContentType "application/json; charset=utf-8"。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-04
    • 1970-01-01
    • 2016-12-14
    • 2021-02-22
    • 2020-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多