Invoke-WebRequest Post Multipart Form

Welcome Forums General PowerShell Q&A Invoke-WebRequest Post Multipart Form

Viewing 3 reply threads
  • Author
    Posts
    • #33361
      Participant
      Topics: 5
      Replies: 323
      Points: 487
      Helping Hand
      Rank: Contributor

      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
      VERBOSE: POST
      http://ipaddress/edit_message with 3608-byte payload
      VERBOSE: received 61701-byte response of content type text/html;charset=UTF-8

      Network 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 ####

    • #33362
      Keymaster
      Topics: 18
      Replies: 4872
      Points: 1,903
      Helping HandTeam Member
      Rank: Community Hero

      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.

    • #33523
      Participant
      Topics: 5
      Replies: 323
      Points: 487
      Helping Hand
      Rank: Contributor

      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.navigate2($url)
      $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'
      $ie.Document.getElementById('_19_publishButton').click() 
      
    • #33527
      Keymaster
      Topics: 18
      Replies: 4872
      Points: 1,903
      Helping HandTeam Member
      Rank: Community Hero

      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.

Viewing 3 reply threads
  • The topic ‘Invoke-WebRequest Post Multipart Form’ is closed to new replies.