title: How to convert PowerShell System.Net.Http.Httpclient call to Invoke-RestMethod
body: <p>I have the following code which is working. But my requirements that I can't use System.Net.Http.Httpclient. I have to convert it to Invoke-RestMethod -Method Post. Following code works fine.      </p>
  
  <pre><code># Add the necessary .NET assembly
  Add-Type -AssemblyName System.Net.Http
  # Create the HttpClient client
  $client = New-Object -TypeName System.Net.Http.Httpclient
  $client.BaseAddress = $sasHost; 
  $action = '/system/collection';
  $stringcontent =  New-Object System.Net.Http.StringContent($json, 
       [System.Text.Encoding]::UTF8, "application/json");
  $response = $client.PostAsync($action, $stringcontent).Result;
  # If it is failed 
  if($response.IsSuccessStatusCode -eq $false)
  {
      write-host $response.StatusCode -foregroundcolor "Red";
  }
  </code></pre>
  
  <p>I tried the following. But it throws an exception. I suspect my conversion is not correct. Because the above code works fine. I don't know how to do it correctly. </p>
  
  <pre><code>$url = ($sasHost, $action  -join "");
  try
  {
      Invoke-RestMethod -Method Post -Uri $url -ContentType "application/json" -Body $stringcontent
      write-host "Collection bootstrapping done successfully" -foregroundcolor DarkMagenta -BackgroundColor Green;
  }
  catch [Exception]
  {
      Write-Host "Exception: "$_.Exception.Message -ForegroundColor Red;
  }
  </code></pre>
  
  <blockquote>
    <p>$_.Exception.Message The remote server returned an error: (500)
    Internal Server Error.</p>
  </blockquote>