Getting the server response when Invoke-Webrequest results in error 5xx

Hi there,

lately, I’ve been confronted with a problem where I simply couldn’t get meaningful error messages from failing runs of Invoke-Webrequest. In doing something like this:

$err would only ever contain the error code like “500 bla”, but not what the server actually returned as the error message. I was querying a REST API.

A colleague finally found the answer here.

What needs to be done is, to tell Powershell to read the stream. The exact difference I cannot tell, as this happens only for 5xx errors, as far as I can see.

$response will now contain the body of what the remote web service returned.

This is another good case that shows me, how easily one can get lost doing g**gle searches. I had searched for a solution also, but the sheer mass of results has overwhelmed me. Also, using only slightly different search terms always give totally different search results… Maybe I was using the wrong terms… Maybe I should have also read the documentation for Invoke-Webrequest more closely 🙂

Leave a Reply

Name *
Email *