title: Invoke-RestMethod Upload ZIP File
body: <p>I try to upload a file to a uri with POST. I somehow fail. I got help from</p>
  
  <p><a href="https://stackoverflow.com/questions/36268925/powershell-invoke-restmethod-multipart-form-data">powershell invoke-restmethod multipart/form-data</a></p>
  
  <p>but they uploaded a text file.</p>
  
  <p>Also the curl command works:</p>
  
  <pre><code>curl -X POST "https://some.place.over.the.rainbow.com/api/v1/dataupdate" -H "accept: */*" -H "Authorization: Basic 123123123123" -F "file=@/c/updates/SomeFile.zip"   
  </code></pre>
  
  <p>In my ps1 Script I tried following:</p>
  
  <pre><code>[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
  
  $Headers = @{
      Authorization = "Basic 123123123123"
  };
  
  $FilePath = "C:\Users\asd\SomeFile.zip";
  $Uri = 'ttps://some.place.over.the.rainbow.com/api/v1/dataupdate';
  $LF = "`r`n";
  $boundary = [System.Guid]::NewGuid().ToString(); 
  
  $fileBytes = [System.IO.File]::ReadAllBytes($FilePath);
  $fileEnc = [System.Text.Encoding]::GetEncoding('UTF-8').GetString($fileBytes);
  
  $bodyLines = ( 
      "--$boundary",
      "Content-Disposition: form-data; name=`"file`"; filename=`"SomeFile.zip`"",
      $fileEnc,
      "--$boundary--$LF" 
  ) -join $LF
  
  Invoke-RestMethod -Uri $Uri -Method Post -ContentType "multipart/form-data; boundary=`"$boundary`"" -Headers $Headers -Body $bodyLines
  </code></pre>
  
  <p>On Serverside I get</p>
  
  <pre><code>Servlet.service() for servlet [dispatcherServlet] in context with path [/...] threw exception [Request processing failed; nested exception is
   org.springframework.web.multipart.MultipartException: Failed to parse multipart servlet request; 
   nested exception is java.io.IOException: org.apache.tomcat.util.http.fileupload.FileUploadException: 
   Header section has more than 10240 bytes (maybe it is not properly terminated)] with root cause
   org.apache.tomcat.util.http.fileupload.MultipartStream$MalformedStreamException: Header section has more than 10240 bytes (maybe it is not properly terminated)at 
   org.apache.tomcat.util.http.fileupload.MultipartStream.readHeaders(MultipartStream.java:523) 
   ~[tomcat-embed-core-8.5.34.jar!/:8.5.34]at org.apache.tomcat.util.http.fileupload.FileUploadBase$FileItemIteratorImpl.findNextItem(FileUploadBase.java:880) 
   ~[tomcat-embed-core-8.5.34.jar!/:8.5.34]at org.apache.tomcat.util.http.fileupload.FileUploadBase$FileItemIteratorImpl.&lt;init&gt;(FileUploadBase.java:845) 
  </code></pre>
  
  <p>I  need to execute it on a windows machine where I do not haver curl unfortunately.</p>
  
  <p>Appreciate any help</p>