Suin Yun
Suin Yun

Reputation: 95

Powershell Invoke-RestMethod Internal Server Error

When I run this (parameters and body that worked from 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:

An error occurred while calling REST method at: 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

Any expert in Powershell, JSON, and REST API that can help me with this issue?

Upvotes: 1

Views: 10422

Answers (2)

Patrice Calvé
Patrice Calvé

Reputation: 684

From PowerShell since you serialize the content to JSON, specify -ContentType "application/json". Also, if you think the content might contain unicode strings, include the -ContentType "application/json; charset=utf-8".

Upvotes: 2

Julien Nury
Julien Nury

Reputation: 307

The content of the Body parameter of Invoke-RestMethod should be an object serialized in JSon. In your example, you have 3 levels of serialization.

You should remove 2 levels of serialization:

$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.

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

Upvotes: 1

Related Questions