in reply to File upload w/WWW::Mechanize

Figured it out, hopefully this helps someone else in the future:

my $form = $agent->form_name('put_form'); $agent->field('file', "/tmp/file.1"); my $input = $form->find_input('file'); $input->filename('myFilename'); $input->headers('Content-Type', 'application/octet-stream');
Fun, fun fun.

JK

Replies are listed 'Best First'.
Re^2: File upload w/WWW::Mechanize
by jczeus (Monk) on Mar 11, 2016 at 14:16 UTC
    It did!

    Just ran into the same problem. Your solution still works.

    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?

    This would be a nice candidate for the cookbook.

        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.