[SOLVED] Connection closed when using Powershell's "Invoke-RestMethod" on playcanvas URLs

I’m writing a post-build powershell script for an ASP.NET application which is going to download a playcanvas project using the Rest API, extract it and make it servable with the rest of the project’s contents.

I would like this to run with no dependencies on tools that aren’t pre-installed with windows.

Powershell has a command ‘Invoke-RestMethod’, an http client which could be used with the API like curl. When calling it on most Uris I get the output I expect:

PS C:\Users\josh.taylor> Invoke-RestMethod -Uri 'https://jsonplaceholder.typicode.com/posts' -Method GET

userId  id title                                                                       
------  -- -----                                                                       
     1   1 sunt aut facere repellat provident occaecati excepturi optio reprehenderit  
     1   2 qui est esse
...
Lots of content                                                                                                                                                         

But when calling it on any playcanvas website uri or API endpoint, e.g. with the following script

try {
    Invoke-RestMethod -Uri 'https://playcanvas.com'
}
catch {
    $_.Exception | Format-List -Force
}

It returns that the connection has closed:

Status         : SendFailure
Response       : 
Message        : The underlying connection was closed: An unexpected error occurred on a send.
Data           : {}
InnerException : System.IO.IOException: Authentication failed because the remote party has closed the 
                 transport stream.
                    at System.Net.TlsStream.EndWrite(IAsyncResult asyncResult)
                    at System.Net.ConnectStream.WriteHeadersCallback(IAsyncResult ar)
TargetSite     : System.Net.WebResponse GetResponse(System.Net.WebRequest)
StackTrace     :    at Microsoft.PowerShell.Commands.WebRequestPSCmdlet.GetResponse(WebRequest request)
                    at Microsoft.PowerShell.Commands.WebRequestPSCmdlet.ProcessRecord()
HelpLink       : 
Source         : Microsoft.PowerShell.Commands.Utility
HResult        : -2146233079

Can anybody else replicate this?

I am also new to the forum, so I’m sorry if this has been placed in the wrong category.

Not sure I can help much because I haven’t used that tool, but there’s some info on how to call the PlayCanvas REST API here: https://developer.playcanvas.com/en/user-manual/api/.

Perhaps if you are not passing the correct parameters or use the correct HTTPS urls something goes wrong with the tool not sure…

The odd thing is that even with non-api web requests (like for the homepage), the powershell command line tools fail. But they succeed on sites like the google homepage. This seems to be an operations issue specific to the playcanvas network.

Maybe - for me curl seems to work fine for example. It could be a header that breaks that tool for some reason perhaps. Maybe @dave or @max know more.

Thanks, I’d love to use curl if I could but I’d rather not add it as another dependency to the build.

Quick googling pointing to the SSL authentication issues. I think it is related to TLS1.1 on our end which seems like the tool is struggling to deal with. But simple google with error you are getting, seems like there are many solutions to it, feel free to check previous cases regarding this in google.

Thanks, I read this stackoverflow answer and added this line to the top of my script:

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

And it now returns fine.