How to get Powershell Invoke-Restmethod to return

2019-01-11 03:31发布

问题:

Invoke-RestMethod call returns only very unhelpful exception below and does not (as far as I can tell) allow you to collect the body content (JSON object shown in fiddler trace results). This seems a pretty bad implementation if so because http 500 definition is pretty specific that client should return the body of the response to help troubleshoot... Am I missing something?

invoke-restmethod -method Post -uri "https://api-stage.enviance.com/ver2/EqlService.svc/eql" -Body (ConvertTo-Json $eqlhash)  -Headers @{"Authorization"="Enviance $session"}

invoke-restmethod : The remote server returned an error: (500) Internal Server Error. At line:1 char:9...

Fiddler trace below

HTTP/1.1 500 Internal Server Error Connection: close Date: Thu, 12 Sep 2013 17:35:00 GMT Server: Microsoft-IIS/6.0 X-Powered-By: ASP.NET X-AspNet-Version: 2.0.50727 EnvApi-Version: 2.0,2.0 EnvApi-Remaining-Calls: 994,994 EnvApi-Remaining-Interval: 2684,2684 Cache-Control: no-cache Pragma: no-cache Expires: -1 Content-Type: text/csv; charset=utf-8

{"errorNumber":0,"message":"Current user has no rights to retrieve data from table 'CustomFieldTemplate'"}

回答1:

Although an old thread, here an answer to the problem with the cmdlets Invoke-WebRequest and Invoke-RestMethod.

This one has bothered me for quite some time. As all 4xx and 5xx responses are generating an exception, you have to catch that one and then you are able to extract the Response from there though. Use it like this:

$resp = try { Invoke-WebRequest ... } catch { $_.Exception.Response }

Now $resp always contains everything you like.



回答2:

The other answer does get you the response, but you need an additional step to get the actual body of the response, not just the headers. Here is a snippet:

try {
        $result = Invoke-WebRequest ...
}
catch {
        $result = $_.Exception.Response.GetResponseStream()
        $reader = New-Object System.IO.StreamReader($result)
        $reader.BaseStream.Position = 0
        $reader.DiscardBufferedData()
        $responseBody = $reader.ReadToEnd();
}


回答3:

Searching an answer for my problem I found this thread.

These solution worked for me, but I had to add two new line:

 $reader.BaseStream.Position = 0
 $reader.DiscardBufferedData()

Thanks!



回答4:

This solution no longer works with PowerShell 6 - it does not support GetResponseStream(). Instead use

try {
    $result = Invoke-WebRequest ...
}
catch {
    $_.ErrorDetails.Message
}

I wrote a short helper function to support PowerShell 6 and earlier:

function ParseErrorForResponseBody($Error) {
    if ($PSVersionTable.PSVersion.Major -lt 6) {
        if ($Error.Exception.Response) {  
            $Reader = New-Object System.IO.StreamReader($Error.Exception.Response.GetResponseStream())
            $Reader.BaseStream.Position = 0
            $Reader.DiscardBufferedData()
            $ResponseBody = $Reader.ReadToEnd()
            if ($ResponseBody.StartsWith('{')) {
                $ResponseBody = $ResponseBody | ConvertFrom-Json
            }
            return $ResponseBody
        }
    }
    else {
        return $Error.ErrorDetails.Message
    }
}

try {
    $result = Invoke-WebRequest ...
}
catch {
    ParseErrorForResponseBody($_)
}