title: Using PowerShell Invoke-RestMethod to POST large binary multipart/form-data
body: <p>I'm trying to use the <a href="http://technet.microsoft.com/en-us/library/hh849971.aspx" rel="nofollow noreferrer">Invoke-RestMethod</a> cmdlet in PowerShell 3 and 4, to upload a large binary file using a REST API's multipart/form-data upload.  Here is a working cURL example on how to perform what I want to do in PowerShell:</p>
  
  <pre class="lang-bsh prettyprint-override"><code>curl -i -k -H "accept: application/json" -H "content-type: multipart/form-data" -H "accept-language: en-us" -H "auth: tokenid" -F file="@Z:\large_binary_file.bin" -X POST "https://server/rest/uri2"
  </code></pre>
  
  <p>I would love to see a working example on how to use Invoke-RestMethod to POST a multipart/form-data.  I found a <a href="http://blogs.technet.com/b/heyscriptingguy/archive/2013/07/02/use-powershell-to-work-with-skydrive-for-powerful-automation.aspx" rel="nofollow noreferrer">blog post from the PowerShell team showing how to use Invoke-RestMethod</a> to upload to OneDrive (aka SkyDrive), but doesn't work well.  I'd also like to avoid using System.Net.WebClient if at all possible.  I also found <a href="https://stackoverflow.com/questions/9139504/using-powershell-v3s-invoke-restmethod-to-put-post-x-mb-of-a-binary-file">another thread here on Stackoverflow</a>, but it really didn't help much. </p>
  
  <pre class="lang-cs prettyprint-override"><code>[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }
  
  $server = "https://server"
  uri = "/rest/uri1"
  $headers = @{"accept" = "application/json"; "content-type" = "application/json";"accept-language" = "en-us"}
  $body = @{"userName" = "administrator"; "password" = "password"}
  $method = "POST"
  
  #Get Session ID
  $resp = Invoke-RestMethod -Method $method -Headers $headers -Uri ($server+$uri) -body (convertto-json $Body -depth 99)
  
  $sessionID = $resp.sessionID
  
  #Upload file
  $uri = "/rest/uri2"
  $headers = @{"accept" = "application/json";"content-type" = "multipart/form-data"; "accept-        language" = "en-us"; "auth" = $sessionID}
  $medthod = "POST"
  $largeFile = "Z:\large_binary_file.bin"
  </code></pre>
  
  <p>I have tried both ways of using Invoke-RestMethod:</p>
  
  <pre class="lang-cs prettyprint-override"><code>Invoke-RestMethod -Method $method -Headers $headers -Uri ($server+$uri) -InFile $largeFile
  </code></pre>
  
  <p>or</p>
  
  <pre class="lang-cs prettyprint-override"><code>$body = "file=$(get-content $updateFile -Enc Byte -raw)"
  Invoke-RestMethod -Method $method -Headers $headers -Uri ($server+$uri) -body $body
  </code></pre>