Please, I need help with uploading excel files by Powershell. I need to write script, that POST existing excel (well-formated according external system rules) to URI of that system. Script need to be authentificate via NTLM (on Apache) and then post data.
I have tried something like this:
Invoke-WebRequest -Uri $uri -Method Post -InFile $filePath -ContentType "multipart/form-data" -Headers @{"Authorization" = "Basic "+[System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($username+":"+$password ))}
where $uri is something like http://[servername]/[applicationame]/apix/marketdata/upload
and $filePath is C:\Users..\Documents\Market_data.xlsx
Error:
Invoke-WebRequest : {"status":"ERROR","message":"HTTP 400 Bad Request","additionalMessages":[]}
At C:\Users\..\script.ps1:48 char:1
+ Invoke-WebRequest -Uri $uri -Method Post -InFile $filePath -ContentTy ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
This Curl.exe command is working, so I need to retype it to PS curl
curl.exe -X POST -H "Pragma: no-cache" -H "Origin: http://[server]" -H "Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW" -H "Accept: */*" -H "Cache-Control: no-cache" -H "X-Requested-With: XMLHttpRequest" -H "Connection: keep-alive" -H "Referer: http://[server]/" -F "Content-Type=application/vnd.ms-excel" -F "File=@$filePath" -F "filename=$filePath" "http://[servername]/[applicationname]/apix/marketdata/upload?User=[DOMAIN]%255C[USER]" --ntlm --negotiate -u 'DOMAIN\user:password'