Invoke-WebRequest Post Multipart Form

What I would like to do is post a multipart form to a messageboard. After submitting the form, the messageboard does not contain the new submitted post. Below is the PowerShell code, verbose output, and a sample network capture of a successful post via Internet Explorer 9. I have successfully logged in and continue to use a session.
I think the ‘-ContentType’ and/or ‘-Body’ parameters are incorrect.

# Fill out form fields
# All other fields in network capture have default values
# ($forms.Fields) TypeName: System.Collections.Generic.Dictionary
$forms.Fields._19_cmd = 'add' 
$forms.Fields._19_subject = 'Subject Test'
$forms.Fields._19_editor = 'Body Test'
$forms.Fields._19_body = 'Body Test'
$forms.Fields._19_workflowAction = '1' 

# Submit Form
$Type = 'multipart/form-data; boundary=---------------------------7df14832203f6'
$submitform = Invoke-WebRequest -Uri $forms.action -Body $forms.Fields -Method Post -WebSession $session1 -ContentType $Type -Verbose

Verbose Output
http://ipaddress/edit_message with 3608-byte payload
VERBOSE: received 61701-byte response of content type text/html;charset=UTF-8

Network Capture


Request: POST /web/action%3D%252Fmessage_boards%252Fedit_message HTTP/1.1
Accept: text/html, application/xhtml+xml, /
Referer: /web/action=%2Fmessage_boards%2Fedit_message
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)
Content-Type: multipart/form-data; boundary=---------------------------7df14832203f6
Accept-Encoding: gzip, deflate
Connection: Keep-Alive Cache-Control: no-cache



Content-Disposition: form-data; name="_19_formDate"

Content-Disposition: form-data; name="_19_cmd"

Content-Disposition: form-data; name="_19_redirect"

Content-Disposition: form-data; name="_19_messageId"

Content-Disposition: form-data; name="_19_mbCategoryId"

Content-Disposition: form-data; name="_19_threadId"

Content-Disposition: form-data; name="_19_workflowAction"

Content-Disposition: form-data; name="_19_subject"

Subject Test
Content-Disposition: form-data; name="_19_editor"

Body Test
Content-Disposition: form-data; name="_19_body"

Body Test
Content-Disposition: form-data; name="_19_msgFile1"; filename=""
Content-Type: application/octet-stream



So is there a reason it needs to be multipart, as opposed to just url encoded? E.g.,

Oh, I see, you’ve got an octet stream in there. You’re trying to submit a file? You’d actually need to read in the file, Base64-encode it, and jam that into the body.

No, I am filling out a form and not submitting a file. I am not understanding something here…maybe with contenttype, body, and/or encoding. I have tried changing the ‘-ContentType’ parameter to the following.
ContentType = ‘application/x-www-form-urlencoded; charset=UTF-8’
ContentType = ‘application/x-www-form-urlencoded’
ContentType = ‘multipart/form-data; boundary=$boundary’

What am I overlooking with Invoke-WebRequest? I can submit the post via the following:

$url = 'url'
$ie = New-Object -ComObject internetexplorer.application
$ie.visible = $false
while ($ie.busy){Start-Sleep -Milliseconds 50}
$ie.Document.getElementById('_19_subject').value = 'test thread name'
$ie.Document.getElementById('_19_editor').value = 'test thread body'

Your second sample is have a browser do all the heavy lifting. The cmdlet isn’t a web browser - there’s a lot more work you have to do. Unfortunately I’m not an http expert - I don’t know what you’re doing wrong. Since what you’re trying really isn’t powershell per se, you might try posting on StackOverflow. There are a lot more people there familiar with how to form a raw http request who’d probably know the answer right off. Sorry I don’t.