title: Powershell WebRequest : the request was aborted: the connection was closed unexpectedly
body: <p>I've found quite a few references to this problem, but just about all of the answers suggested to either enable tls12 or turn off the certificate check.  I have the tls12 enabled and it's not working.  I did try turning off the certificate check to see if it'd work and it didn't.  I feel like that wouldn't be a good idea to go to production anyways.</p>
  <p>I'm trying to do a post request in a powershell script.  I've been able to do a GET request, but the post requests keep failing.</p>
  <p>Powershell version is 5.1</p>
  <pre><code>[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
  [System.Collections.ArrayList]$ids = @{}
  
  $ids.Add(10)
  $ids.Add(15)
  $ids.Add(20)
  Write-Output $ids
  
  $loadToken = &quot;token&quot;
  $loadUrl = &quot;https://example.com/token/load/&quot; + $loadToken
  $token = Invoke-RestMethod -Method GET -Uri $loadUrl
  
  Write-Output $token.token
  
  $printToken = &quot;print token&quot;
  $printUrl = &quot;https://example.com/post-route/&quot; + $printToken
  
  $params = @{
  _token = $token.token
  ids = $a
  } | ConvertTo-Json
  
  Write-Output $params
  
  $response = Invoke-WebRequest -Uri $printUrl -Method Post -Body $params -ContentType &quot;application/json&quot;
  Write-Output $response
  </code></pre>
  <p>This is the output that I'm getting:</p>
  <pre><code>0
  1
  2
  10
  15
  20
  {loaded token}
  {
      &quot;ids&quot;:  [
                      10,
                      15,
                      20
                  ],
      &quot;_token&quot;:  &quot;token value&quot;
  }
  
  Invoke-WebRequest : The request was aborted: The connection was closed unexpectedly.
  At C:\path\to\script\test.ps1:28 char:13
  + $response = Invoke-WebRequest -Uri $printUrl -Method Post -Body $para ...
  +           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo        : InvalidOperation: (System.Net.HttpWebRequest) [Invoke-WebRequest], WebException
      + Fully QualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestWebRequestCommand
  
  </code></pre>
  <p>Edit: updated urls to be clear that web requests are for the same domain.</p>