Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Powershell 6+ (in this case v7) - GetResponseStream #4

Open
5M7X opened this issue May 27, 2021 · 1 comment
Open

Powershell 6+ (in this case v7) - GetResponseStream #4

5M7X opened this issue May 27, 2021 · 1 comment

Comments

@5M7X
Copy link

5M7X commented May 27, 2021

$resp = $_.Exception.Response.GetResponseStream() this line causes a problem as GetResponseStream() seems to be deprecated since powershell v6+.

I ran into this error with the latest powershell v7.
A possible fix would be a wrapper like this one here: https://stackoverflow.com/a/48154663/16052146

    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($_)
}

Tried it out but it then breaks

710 | $reader = New-Object System.IO.StreamReader($resp)

@5M7X
Copy link
Author

5M7X commented May 27, 2021

getresponsestatus_powershell7

getresponsestatus_powershell7_version

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant