Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

File upload w/WWW::Mechanize

by jkenneth (Pilgrim)
on Oct 30, 2003 at 13:54 UTC ( #303259=perlquestion: print w/replies, xml ) Need Help??

jkenneth has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to use Mech to automate a file upload, however I'm seeing some odd behavior. When I execute the upload via the browser the params are:

Content-Disposition: form-data; name="file"; filename="file.1" Content-Type: application/octet-stream

However via Mech they send:

Content-Disposition: form-data; name="file"; filename="/tmp/file.1" Content-Length: 1056 Content-Type: application/x-troff-man

While reading the docs I found how to do it using HTTP::Request::Common, so after digging I tried to get the following to work:

use WWW::Mechanize 0.63; use WWW::Mechanize::FormFiller; $agent = WWW::Mechanize->new(); $agent->get("http://moonwolf/file2.html"); $agent->form_name('put_form'); $agent->field('file', ["/tmp/file.1", 'myFilename', ['Content-Type' => 'application/octect-stream']], 1); $agent->click();

Unfortunately, this causes the filename to become an array reference.

I tracked down the name to HTML::Form::FileInput where it has a reference to a 'legacy' section of code, which when I added

$filename = $fn if (ref($filename));

fixed the issue.

Is there another way to do this using WWW::Mechanize? I'd rather not have my change get whacked if someone decides to update LWP in the future.

JK

Replies are listed 'Best First'.
Re: File upload w/WWW::Mechanize
by Corion (Pope) on Oct 30, 2003 at 16:57 UTC

    What the client sends as the filename is more or less undefined. You should always strip the path information from the filenames in your CGI scripts.

    If the site you're automating is not under your control, you have two choices: Either write the author of HTML::Form with a bug report and possibly a test program exercising the bug, or patch HTML::Form dynamically in your code, assuming that the sub was originally called 'legacy_code' :

    use HTML::Form; { no warnings 'redefine'; *HTML::Form::FileInput::legacy_code = sub { ... fixed code ... };
    perl -MHTTP::Daemon -MHTTP::Response -MLWP::Simple -e ' ; # The $d = new HTTP::Daemon and fork and getprint $d->url and exit;#spider ($c = $d->accept())->get_request(); $c->send_response( new #in the HTTP::Response(200,$_,$_,qq(Just another Perl hacker\n))); ' # web
Re: File upload w/WWW::Mechanize
by jkenneth (Pilgrim) on Oct 31, 2003 at 17:49 UTC
    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

      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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://303259]
Approved by bart
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2021-10-21 11:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (83 votes). Check out past polls.

    Notices?