title: Invoke-WebRequest / Invoke-RestMethod failed with error underlying connection closed
body: <p>I have an existing REST API that accept x-www-form-urlencoded. The API need parameter apikey, and tested successfully in Postman as shown below.</p>
  
  <p><a href="https://i.stack.imgur.com/FErMe.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/FErMe.png" alt="enter image description here"></a></p>
  
  <p>However I need to invoke this API using Powershell.Below is my code :</p>
  
  <pre><code>$params = @{"apikey"="abcd1234"}
  Invoke-WebRequest -Uri http://localhost:3030/api/v1/usergroupsync -Method POST -Body $params
  #also tried below, no avail.
  Invoke-RestMethod -Uri http://localhost:3030/api/v1/usergroupsync -Method POST -Body $params
  </code></pre>
  
  <p>However I encountered this error :</p>
  
  <pre><code>Invoke-WebRequest : The underlying connection was closed: An unexpected error occured on a receive At line:14 char:1
  + Invoke-WebRequest -Uri http://localhost:3030/api/v1/usergroupsync -Method POST -...
  +==============================================================================
    + CategoryInfo : InvalidOperations: (System.Net.HttpWebRequest:HTTTpWebRequest) [Invoke-WebRequest], WebException
    + FullyQualifiedErrorId : WebcmdletWebResponseException,Microsoft.Powershell.Commands.InvokeWebRequest
  </code></pre>
  
  <p>If I remove -Body, there is no error, and Response was as expected "API Key is not valid" which means my REST API validate correctly. 
  <a href="https://i.stack.imgur.com/bHsVR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/bHsVR.png" alt="enter image description here"></a></p>
  
  <p>So I suspect the reason if my issue is on the body? Any idea on how to solve this issue?</p>
  
  <p>PS Version is 4.0.0</p>
  
  <pre><code>PS C:\&gt; $PSVersionTable.PSVersion
  
  Major  Minor  Build  Revision
  -----  -----  -----  --------
   4      0      -1     -1
  </code></pre>