Windows: Invoke-RestMethod =>The underlying connection was closed: An unexpected error occurred on a receive.

Hi,

Invoke-RestMethod or Invoke-WebRequest uses by default TLS 1.0. This protocal is outdated and would be not accepted by the most Webservers anymore.

Usually this results in the following error message:
The underlying connection was closed: An unexpected error occurred on a receive.

Solution: Set the protocol version before calling those command-lets.

PS D:\> [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
PS D:\> Invoke-RestMethod -Method "GET" -Uri "https://myHOst.myDomain.org/v1/myAPICall" -Headers $headers

Michael

Leave a Reply