1

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?

Suin Yun
  • 95
  • 2
  • 9
  • Why are you converting your json? It serves no purpose. – Maximilian Burszley Jun 14 '18 at 01:19
  • Nobody can help you with an "500 Internal Server Error". Use a different tool (such as [Postman](https://www.getpostman.com/)) and get the request to work manually. Once you have something that definitely works, you can easily re-create that in Powershell. – Tomalak Jun 14 '18 at 07:00

2 Answers2

1

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

Julien Nury
  • 297
  • 2
  • 14
  • Thank you Julien for your helpful insight! I changed my code up like yours, and the string $jsonbody is actually the body that worked from postman, However, i still get this error: The remote server returned an error: (500) Internal Server Error. At line:26 char:5 + Throw $_.Exception + ~~~~~~~~~~~~~~~~~~ + CategoryInfo : OperationStopped: (:) [], WebException + FullyQualifiedErrorId : The remote server returned an error: (500) Internal Server Error. – Suin Yun Jun 14 '18 at 13:45
  • Have you tried the **try {} catch {}** part to get more information about the error ? You should have an output like that: `StatusCode: 500`, `StatusDescription: xxx` – Julien Nury Jun 14 '18 at 13:48
  • I did run your try{} catch{} lines. I am not sure why it didn't return an output like "StatusCode"... any ideas? – Suin Yun Jun 14 '18 at 13:50
  • The StatusCode: and StatusDescription: should appear before the Error. Could you try to remove the line `Throw $_.Exception` to better view the output ? – Julien Nury Jun 14 '18 at 13:52
  • oh yes my bad. It says StatusCode: 500 StatusDescription: Internal Server Error – Suin Yun Jun 14 '18 at 13:58
  • I updated the _try {} catch {}_ block in my answer. Can you try with this new version ? – Julien Nury Jun 14 '18 at 14:20
  • i just did, and it now says PS C:\Windows\system32> if (Get-Member -InputObject $_.Exception -Name 'Response') { Missing closing '}' in statement block or type definition. + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : MissingEndCurlyBrace – Suin Yun Jun 14 '18 at 14:31
  • i know that your code is not missing a } though. so im not sure why it would give such problems – Suin Yun Jun 14 '18 at 14:31
  • here, im about to edit my question! hopefully that will give you a better view – Suin Yun Jun 14 '18 at 14:34
  • OK, so now you get the full message from the server and there is nothing in it that can help to resolve your problem :-/ You may try to contact the owner of the server and ask for server logs. – Julien Nury Jun 14 '18 at 14:44
1

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

Patrice Calvé
  • 644
  • 6
  • 12