0

I try to upload a file to a uri with POST. I somehow fail. I got help from

powershell invoke-restmethod multipart/form-data

but they uploaded a text file.

Also the curl command works:

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"   

In my ps1 Script I tried following:

[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

On Serverside I get

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.<init>(FileUploadBase.java:845) 

I need to execute it on a windows machine where I do not haver curl unfortunately.

Appreciate any help

kism3t
  • 1,343
  • 1
  • 14
  • 33
  • See if this one is more helpful... https://stackoverflow.com/questions/25075010/upload-multiple-files-from-powershell-script – postanote May 13 '19 at 19:41
  • I tried the approach from your link, but I get an different error that the `exceeded the limit for maxPostSize set on the associated connector`, but on my server side spring boot application I set aleardy `spring.servlet.multipart.max-file-size=-1 spring.servlet.multipart.max-request-size=-1` – kism3t May 15 '19 at 06:48
  • try to change encoding from utf-8 to iso8859-1 $fileEnc = [System.Text.Encoding]::GetEncoding('UTF-8').GetString($fileBytes); – ncowboy Jun 21 '19 at 02:31

0 Answers0