4

I am using Powershell's Invoke-WebRequest (this also applies to Invoke-RestMethod) to make a call to an ElasticSearch cluster. This specific command often returns an error result (409 conflict).

Powershell sees the error state, throws an error and does not pass anything useful through the pipeline, and it spews the body of the response onto the console:

enter image description here

However, a) even though it's got an error code, I don't really care that it's errored (only that it's returned), and b) I want to be able to access the body of that response so that I can actually inspect the data contained within.

Is there any way to have PowerShell suppress the error output (-ErrorAction does not work) and pass something useful along the pipeline?

As a note, I actually want to invoke this with Invoke-RestMethod but as it and Invoke-WebRequest are essentially the same command, I've generalised this question to Invoke-WebRequest

3
  • I don't think you can do that with Invoke-WebRequest, you have to go deeper and use System.Net.WebRequest directly, I think I have some code somewhere where I do exactly what you need, is that an option? Commented Aug 8, 2016 at 16:59
  • Totally an option, if you have something you can share. I don't want you to bother writing something just for this though. Commented Aug 8, 2016 at 17:00
  • Beginning with PowerShell 7 Microsoft has added a -SkipHttpErrorCheck parameter to Invoke-WebRequest. Ref: Invoke-WebRequest # -SkipHttpErrorCheck Commented Feb 22, 2022 at 5:53

1 Answer 1

5

Last year I wrote a script to diagnose common IIS setup problems, it's on GitHub, for it I needed to analyse the response from the server, even for non-200 results, especially for non-200 results, so Invoke-WebRequest didn't work.

I removed some stuff and the essential code is below.

I use the .NET System.Net.WebRequest because even if a System.Net.WebException is thrown, you can still access the response-stream with reponse headers and the body.

$url = "http://localhost"

try {
    $r = [System.Net.WebRequest]::Create($url)
    $r.UserAgent=$userAgent
    $resp = $r.GetResponse()        

    Write-host "Response Headers:" 
    foreach ($HeaderKey in $resp.Headers) {
            $caption = $HeaderKey.PadLeft(15," ")
            Write-host "$caption`: $($resp.Headers[$HeaderKey])";
    }                            

    $reqstream = $resp.GetResponseStream()
    $sr = New-Object System.IO.StreamReader $reqstream
    $body = $sr.ReadToEnd()
    Write-host "$body"

    $resp.StatusCode 
}           
catch [System.Net.WebException] 
{              
    $resp = $_.Exception.Response

    if ($resp -eq $null)
    {
        Write-host $_.Exception
    }
    else
    {
        $reqstream = $resp.GetResponseStream()
        $sr = New-Object System.IO.StreamReader $reqstream
        $body = $sr.ReadToEnd()

        Write-host -Text "Response Headers:" 
        Echo "         Status: $([int]$resp.StatusCode) - $($resp.StatusCode)"
        foreach ($HeaderKey in $resp.Headers) {
                $caption = $HeaderKey.PadLeft(15," ")
                Write-host "$caption`: $($resp.Headers[$HeaderKey])";
        }
        Write-host "$body"

        $resp.StatusCode                
    }                    

} catch {            
    Write-host  $_.Exception
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .