I've got a REST API endpoint in .net core 3.1 that looks like this:
[Route("Install")]
[HttpPost]
[RequestSizeLimit(524288000)]
public async Task<IActionResult> AddInstallFile(List<IFormFile> files)
I can upload a file using cURL with this command:
curl -X POST MYURL Content-Type: multipart/form-data" -F "files=@filename.zip;type=application/x-zip-compressed"
It also works via swagger
In powershell though Multipart/form-data is not natively supported so I'm using the following code to upload there (essentially I used the working swagger upload with 'Copy as Powershell' function in chrome dev tools to create this code):
$fileBytes = [System.IO.File]::ReadAllBytes("C:\Temp\filename.zip");
$enc = [System.Text.Encoding]::GetEncoding('utf-8')
$fileEnc = $enc.GetString($fileBytes)
Invoke-WebRequest -Uri "MYURL" `
-Method "POST" `
-Headers @{
"method"="POST"
"accept"="application/octet-stream"
"accept-encoding"="gzip, deflate, br"
} `
-ContentType "multipart/form-data; boundary=----WebKitFormBoundaryT2XycANuthCIUwGk" `
-Body ([System.Text.Encoding]::UTF8.GetBytes("------WebKitFormBoundaryT2XycANuthCIUwGk$([char]13)$([char]10)Content-Disposition: form-data; name=`"files`"; filename=`"filename.zip`"$([char]13)$([char]10)Content-Type: application/x-zip-compressed$([char]13)$([char]10)$([char]13)$([char]10)$fileEnc$([char]13)$([char]10)------WebKitFormBoundaryT2XycANuthCIUwGk--$([char]13)$([char]10)"))
This Powershell code works fine for a zip file that I have that is under 2MB but fails for another zip file that is over 80MB
Error on the server is here:
An unhandled exception has occurred while executing the request.
Exception:
System.ArgumentNullException: Value cannot be null. (Parameter 'source')
at System.Linq.ThrowHelper.ThrowArgumentNullException(ExceptionArgument argument)
at System.Linq.Enumerable.Sum[TSource](IEnumerable`1 source, Func`2 selector)
at netproject.ControllerClass.AddInstallFile(List`1 files) in ControllerClass.cs:line 44
Essentially the files variable is coming through as null when called from powershell for a file over ~50MB. I've tried both Invoke-WebRequest and Invoke-RestMethod with the same results