title: Rest call with PUT method in powershell version 2 (.Net) with content body
body: <p>Here is my REST call (PUT) with powershell version 2 (using .net class System.Net.HttpWebRequest)</p>
  
  <p>The call has a body which is an XML file and i can make this work in powershell version 3 and above using invoke-WebRequest with the property "Body". But i have to do this with .net class as we have some powershell version 2 systems.</p>
  
  <p>Below is my code</p>
  
  <pre><code> $psuri = "$restUri"
  [string]$psdata = Get-Content -path "$myfile.xml"
  [System.Net.HttpWebRequest]$global:ps = [System.Net.HttpWebRequest] [System.Net.WebRequest]::Create("$psuri");
  
  $psbuffer = [System.Text.Encoding]::UTF8.GetBytes($psdata)
  $ps.ContentLength = $psdata.length
  $ps.ContentType = "application/xml"
      [System.IO.Stream]$outputStream = [System.IO.Stream]$ps.GetRequestStream()
      $outputStream.Write($psdata, 0, $psbuffer.Length)
  
  
  
  
  $ps.method = "PUT"
  $ps.Accept = "application/xml"
  $ps.AllowAutoRedirect = $false
  $ps.KeepAlive = $true
  $ps.CookieContainer = $CookieContainer
  $ps.PreAuthenticate = $true
  
          #Try
          #{
  
              [System.Net.HttpWebResponse]$psresponse = $ps.GetResponse() 
  </code></pre>
  
  <p>The same fails with the error <strong>Exception calling "GetRequestStream" with "0" argument(s): "Cannot send a content-body with this verb-type."At line:8 char:9</strong></p>