Welcome › Forums › General PowerShell Q&A › Invoke-WebRequest Post Multipart Form
- This topic has 3 replies, 2 voices, and was last updated 5 years ago by
Keymaster.
-
AuthorPosts
-
-
December 29, 2015 at 11:48 am #33361
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.PowerShell123456789101112# 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 -VerboseVerbose Output
VERBOSE: POST
http://ipaddress/edit_message with 3608-byte payload
VERBOSE: received 61701-byte response of content type text/html;charset=UTF-8Network Capture
#### REQUEST HEADERS ####
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
#### REQUEST HEADERS ######## REQUEST BODY ####
—————————–7df14832203f6
Content-Disposition: form-data; name=”_19_formDate”1450725819960
—————————–7df14832203f6
Content-Disposition: form-data; name=”_19_cmd”add
—————————–7df14832203f6
Content-Disposition: form-data; name=”_19_redirect”http://ipaddress/view&p_p_col_id=column-2&p_p_col_count=1
—————————–7df14832203f6
Content-Disposition: form-data; name=”_19_messageId”0
—————————–7df14832203f6
Content-Disposition: form-data; name=”_19_mbCategoryId”0
—————————–7df14832203f6
Content-Disposition: form-data; name=”_19_threadId”0
—————————–7df14832203f6
Content-Disposition: form-data; name=”_19_workflowAction”1
—————————–7df14832203f6
Content-Disposition: form-data; name=”_19_subject”Subject Test
—————————–7df14832203f6
Content-Disposition: form-data; name=”_19_editor”Body Test
—————————–7df14832203f6
Content-Disposition: form-data; name=”_19_body”Body Test
—————————–7df14832203f6
Content-Disposition: form-data; name=”_19_msgFile1″; filename=””
Content-Type: application/octet-stream—————————–7df14832203f6
#### REQUEST BODY #### -
December 29, 2015 at 11:55 am #33362
So is there a reason it needs to be multipart, as opposed to just url encoded? E.g., http://stackoverflow.com/questions/8417224/how-to-mimic-an-html-form-submission-in-a-post-request
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.
-
January 5, 2016 at 5:01 pm #33523
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:
PowerShell12345678$url = 'url'$ie = New-Object -ComObject internetexplorer.application$ie.navigate2($url)$ie.visible = $falsewhile ($ie.busy){Start-Sleep -Milliseconds 50}$ie.Document.getElementById('_19_subject').value = 'test thread name'$ie.Document.getElementById('_19_editor').value = 'test thread body'$ie.Document.getElementById('_19_publishButton').click() -
January 5, 2016 at 7:41 pm #33527
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.
-
-
AuthorPosts
- The topic ‘Invoke-WebRequest Post Multipart Form’ is closed to new replies.