title: PowerShell WebRequest POST
body: <p>In Windows PowerShell 3.0 was introduced <a href="http://technet.microsoft.com/en-us/library/hh849971.aspx">Invoke-RestMethod</a> cmdlet.</p>
  
  <p><a href="http://technet.microsoft.com/en-us/library/hh849971.aspx">Invoke-RestMethod</a> cmdlet accepts <code>-Body&lt;Object&gt;</code> parameter for setting the body of the request.</p>
  
  <p>Due to a certain limitations <a href="http://technet.microsoft.com/en-us/library/hh849971.aspx">Invoke-RestMethod</a> cmdlet could not be used in our case. From the other hand, an alternative solution described in article <a href="http://blogs.technet.com/b/heyscriptingguy/archive/2013/10/21/invokerestmethod-for-the-rest-of-us.aspx">InvokeRestMethod for the Rest of Us</a> suits our needs:</p>
  
  <pre><code>$request = [System.Net.WebRequest]::Create($url)
  $request.Method="Get"
  $response = $request.GetResponse()
  $requestStream = $response.GetResponseStream()
  $readStream = New-Object System.IO.StreamReader $requestStream
  $data=$readStream.ReadToEnd()
  if($response.ContentType -match "application/xml") {
      $results = [xml]$data
  } elseif($response.ContentType -match "application/json") {
      $results = $data | ConvertFrom-Json
  } else {
      try {
          $results = [xml]$data
      } catch {
          $results = $data | ConvertFrom-Json
      }
  }
  $results 
  </code></pre>
  
  <p>But it is intended for a GET method only.
  Could you please suggest how to extend this code sample with the ability to send the body of the request using <code>POST</code> method (similar to <code>Body</code> parameter in <code>Invoke-RestMethod</code>)?</p>