I have a webapplication that can process POSTing of a html form like this:
<form action="x" method="post" enctype="multipart/form-data">
<input name="xfa" type="file">
<input name="pdf" type="file">
<input type="submit" value="Submit">
</form>
Note that there are two type="file"
<input>
elements.
How can I script POSTing this from a Powershell script? I plan to do that to create a simple test-framework for the service.
I found WebClient.UploadFile(), but that can only handle a single file.
Thank you for taking your time.
I was bothered by this thing and haven't found a satisfactory solution. Although the gist here proposed can do the yob, it is not efficient in case of large files transmittal. I wrote a blog post proposing a solution for it, basing my cmdlet on HttpClient class present in .NET 4.5. If that is not a problem for you, you can check my solution at the following address http://blog.majcica.com/2016/01/13/powershell-tips-and-tricks-multipartform-data-requests/
EDIT:
Cheers
I've remixed @akauppi's answer into a more generic solution, a cmdlet that:
Get-ChildItem
for files to upload-Credential
parameter-FilesKey
parameter to specify the formdata key for the files upload part-WhatIf
-Verbose
loggingIt can be called like this:
Here's the code to the full cmdlet:
I have found a solution to my problem after studying how multipart/form-data is built. A lot of help came in the form of http://www.paraesthesia.com/archive/2009/12/16/posting-multipartform-data-using-.net-webrequest.aspx.
The solution then is to build the body of the request up manually according to that convention. I have left of niceties like correct Content-Lengths etc.
Here is an excerpt of what I am using now:
I've been crafting multipart HTTP POST with PowerShell today. I hope the code below is helpful to you.
Invoke-RestMethod
requires PowerShell 3.0 but the code in the latter of the above links shows how to do HTTP POST with .NET directly, allowing you to have this running in Windows XP as well.Good luck! Please tell if you got it to work.