Forcing multipart/form-data on POST with no files to upload.

899 views
Skip to first unread message

Jonathan Williamson

unread,
Nov 14, 2013, 9:30:03 AM11/14/13
to guz...@googlegroups.com
I have an annoying form that I can use with my web browser to POST to, that I cannot replicate using guzzle at the moment.

The form requires the content-type to be multipart/form-data.

Using firebug I can see that the form request/response headers and body look like this:

  1. Request Headers
    1. Accept:
      text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
    2. Accept-Encoding:
      gzip,deflate,sdch
    3. Accept-Language:
      en-US,en;q=0.8
    4. Cache-Control:
      max-age=0
    5. Connection:
      keep-alive
    6. Content-Length:
      849
    7. Content-Type:
      multipart/form-data; boundary=----WebKitFormBoundaryy0aDN3TK2YELUZ09
    8. Cookie:
      auth=5e6b3363d9c787f54abe9da01563e6bb
    9. DNT:
      1
    10. Host:
    11. Origin:
    12. Referer:
    13. User-Agent:
      Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.48 Safari/537.36


  1. Request Payload
    1. ------WebKitFormBoundaryy0aDN3TK2YELUZ09 Content-Disposition: form-data; name="state" 5 ------WebKitFormBoundaryy0aDN3TK2YELUZ09 Content-Disposition: form-data; name="blank" 0 ------WebKitFormBoundaryy0aDN3TK2YELUZ09 Content-Disposition: form-data; name="aircraftType" A320 ------WebKitFormBoundaryy0aDN3TK2YELUZ09 Content-Disposition: form-data; name="airport" 5 ------WebKitFormBoundaryy0aDN3TK2YELUZ09 Content-Disposition: form-data; name="print" sf-1204 ------WebKitFormBoundaryy0aDN3TK2YELUZ09 Content-Disposition: form-data; name="print" sf-1206 ------WebKitFormBoundaryy0aDN3TK2YELUZ09 Content-Disposition: form-data; name="print" sf-1015 ------WebKitFormBoundaryy0aDN3TK2YELUZ09 Content-Disposition: form-data; name="action_print" Open Selected Information ------WebKitFormBoundaryy0aDN3TK2YELUZ09--


  1. Response Headersview source
    1. Cache-Control:
      must-revalidate, post-check=0, pre-check=0
    2. Connection:
      Keep-Alive
    3. Content-disposition:
      attachment;filename=output.pdf
    4. Content-Type:
      application/pdf
    5. Date:
      Thu, 14 Nov 2013 13:01:45 GMT
    6. Keep-Alive:
      timeout=5, max=100
    7. Pragma:
      public
    8. Server:
      Apache
    9. Transfer-Encoding:
      chunked




In my class I have attempted to recreate this using the following code:

        $request = $this->client->post(
            array('Content-Type'=>'multipart/form-data'),
            array(
                'state' => 5,
                'blank' => 1,
                'aircraftType' => 'A320,
                'airport' => $airport,
                'print' =>'sf-1204',
                'print' =>'sf-1206',
                'print' =>'sf-1015',
                'action_print' => 'Open Selected Information'
            )
        );

        $response = $request->send()->getBody(true);


This actually works - in that I do get a response back (pdf file) but the major problem is that due to the fact there are 3 "print" variables to be sent (sf-204, sf-1206, sf-1015) - you obviously can't have 3 "print" keys in an array, so only one is sent to the server.

Just how can I create the multipart/form-data message body on my request in the same way that the browser can do. I'm a little lost and need a push in the right direction!

Thanks for any help!
Reply all
Reply to author
Forward
0 new messages