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


In reply to File upload w/WWW::Mechanize by jkenneth

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.