Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell web request without throwing exception on 4xx/5xx

Tags:

powershell

I'm writing a powershell script that needs to make a web request and inspect the status code of the response.

I have tried writing this:

$client = new-object system.net.webclient

$response = $client.DownloadData($url)

as well as this:

$response = Invoke-WebRequest $url

but whenever the web page has a status code that's not a success status code, PowerShell goes ahead and throws an exception instead of giving me the actual response object.

How can I get the status code of the page even when it fails to load?

like image 992
jcarpenter2 Avatar asked Oct 01 '13 17:10

jcarpenter2


3 Answers

Try this:

try { $response = Invoke-WebRequest http://localhost/foo } catch {
      $_.Exception.Response.StatusCode.Value__}

It is kind of a bummer that this throws an exception but that's the way it is.

Update per comments

To ensure that such errors still return a valid response, you can capture those exceptions of type WebException and fetch the related Response.

Since the response on the exception is of type System.Net.HttpWebResponse, whilst the response from a successful Invoke-WebRequest call is of type Microsoft.PowerShell.Commands.HtmlWebResponseObject, to return a compatible type from both scenarios we need to take the successful response's BaseResponse, which is also of type System.Net.HttpWebResponse.

This new response type's status code an enum of type [system.net.httpstatuscode], rather than a simple integer, so you have to explicity convert it to int, or access it's Value__ property as described above to get the numeric code.

#ensure we get a response even if an error's returned
$response = try { 
    (Invoke-WebRequest -Uri 'localhost/foo' -ErrorAction Stop).BaseResponse
} catch [System.Net.WebException] { 
    Write-Verbose "An exception was caught: $($_.Exception.Message)"
    $_.Exception.Response 
} 

#then convert the status code enum to int by doing this
$statusCodeInt = [int]$response.BaseResponse.StatusCode
#or this
$statusCodeInt = $response.BaseResponse.StatusCode.Value__
like image 170
Keith Hill Avatar answered Nov 18 '22 00:11

Keith Hill


Since Powershell version 7.0 Invoke-WebRequest have -SkipHttpErrorCheck switch parameter.

-SkipHttpErrorCheck

This parameter causes the cmdlet to ignore HTTP error statuses and continue to process responses. The error responses are written to the pipeline just as if they were successful.

This parameter was introduced in PowerShell 7.

docs pull request

like image 45
Mariusz Pawelski Avatar answered Nov 18 '22 00:11

Mariusz Pawelski


-SkipHttpErrorCheck is the best solution for PowerShell 7+, but if you can't use that yet then here is a simple alternative that is useful for interactive, command-line Poweshell sessions.

When you see the an error description for a 404 response, i.e.,

The remote server returned an error: (404) Not Found.

Then you can see the 'last error' from the command-line by entering this:

$Error[0].Exception.Response.StatusCode

Or

$Error[0].Exception.Response.StatusDescription

Or whatever else you would like to know from the `Response’ object.

like image 5
DaveC Avatar answered Nov 17 '22 22:11

DaveC