in reply to Re^2: File upload w/WWW::Mechanize
in thread File upload w/WWW::Mechanize


Well, jkenneth was last here (8 years ago), so I'll answer

But isn't there another solution? Why do I have to reach into the innards of $agent to specify the file path, the submitted filename and the content type?

You're not reaching into the innards of $agent, you're shaking hands as intended

Anyway, you don't have to go through HTML::Form to upload a file, you can just do it the LWP way, the docs know all,, Re: PUT a Multipart request in PERL ( )

Replies are listed 'Best First'.
Re^4: File upload w/WWW::Mechanize
by c-alpha (Initiate) on Dec 30, 2019 at 17:16 UTC

    With recent versions of WWW::Mechanize (only tried 1.91 or later), there is a simpler solution:

    $response = $mech->submit_form( with_fields => { 'filechooserelement' => [ [ $tmpfile, $uploadfile ], 1 ], ... , }, );

    Where tmpfile is the pathname to the file in your file system, and uploadfile is the filename that will be given in the in the multipart request to the server (be sure to never include a path here). WWW::Mechanize takes care of whether it's a PUT or POST, etc. filechooserelement must be a form field of type file to trigger this behaviour, of course.

      WWW::Mechanize takes care of whether it's a PUT or POST, etc


      PUT is a HTTP method, and its one not supported by html forms. Mechanize submit_form doesn't support it either since HTML::Form doesn't. HTML::Form simply passes along upload filename to LWP family (see link above). LWP (HTTP....) does not support multipart/form-data for PUTs , only for POSTS....