The underlying connection was closed

For the past weeks I’ve used PowerShell to test REST APIs exposed over HTTPS. Everything was wonderful until I had to execute my script against a local instance of the service. My machine is not setup with proper certificates and PowerShell doesn’t see to like that. When ever I execute an Invoke-RestMethod command I get the following error message:

The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel

Looking around I was fortunate to find this one-liner that fixes everything!

[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}

By turning off certificate validation you are disabling security. Beware that this one-liner is a patch, and should not be used for anything else than development purposes.

4 responses to #PowerShell Invoke-RestMethod – the underlying connection was closed

  1. 

    It is usually better to apply this trick conditionally, for example http://dzimchuk.net/post/Disable-SSL-certificate-validation-during-development

    For scripts we should probably check URLs to decide if we want to skip certificate validation.

    Like

Trackbacks and Pingbacks:

  1. Reading Notes 2014-09-15 | Matricis - September 15, 2014

    […] #PowerShell Invoke-RestMethod – the underlying connection was closed – Useful tips to save some headache while developing. […]

    Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.