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

I've been using a WWW::Mechanize script for a couple years to automate some maintenance activities for one of our products (Lyris ListManager, if anyone happens to be familiar). After upgrading the app to a newer version, my script stopped working, though it's not actually "failing" as far as the script is concerned - it's just not doing what it's intended to do. I've been doing tcpdumps and such, and the only notable difference I can find is that the file I'm uploading via the web form has a different content-type via Mechanize vs a browser. Is there a way I can specify a file type for a "file" form field? I don't know if it really matters, as the data being sent is identical in both traces, but it's all I can come up with so far. I'm far from a Mechanize expert, and I'm struggling to troubleshoot further.

The submit button is a linked image, and I'm using click() to call it, specifying the name and some coordinates I stole from a tcpdump from a web session. I didn't have to use coordinates previously, so I'm not entirely sure if I can just pick coords like that, or if they are dynamic and relative to the session somehow.

Below are snippets of the POST, indicating the only notable differences I could find.

Anyone have any ideas? I can post the entire POST if needed...

Working:

-----------------------------2995119424827 Content-Disposition: form-data; name="file_name"; filename="test.csv" Content-Type: application/vnd.ms-excel <data here> -----------------------------2995119424827 Content-Disposition: form-data; name="import.x" 22 -----------------------------2995119424827 Content-Disposition: form-data; name="import.y" 3 -----------------------------2995119424827--

Not Working:

--xYzZY Content-Disposition: form-data; name="file_name"; filename="<path>/tes +t.csv" Content-Type: text/plain <data here> --xYzZY Content-Disposition: form-data; name="import.x" 20 --xYzZY Content-Disposition: form-data; name="import.y" 9 --xYzZY--

Replies are listed 'Best First'.
Re: WWW::Mechanize issue
by ikegami (Patriarch) on Apr 08, 2009 at 16:14 UTC
    my $input = $m->current_form()->find_input('file_name'); $input->headers( content_type => $content_type );

    HTML::Form

Re: WWW::Mechanize issue
by almut (Canon) on Apr 08, 2009 at 17:37 UTC

    BTW, HTML::Form objects also have a click() method, which returns an HTTP::Request object (in contrast to $mech->click(...), which returns an HTTP::Response object). And the HTTP::Request object has an as_string() method, that can be used to debug such things without doing tcpdumps (or even actually sending the request). For example:

    use HTML::Form; my $html = <<'EOHTML'; <html> <body> <form action="http://localhost/" method="POST" enctype="multipart/form +-data"> <input type="file" name="file_name" value="test.csv" /> <input type="Submit" name="submit" value="Submit" label="Sumbit" /> </form> </body> </html> EOHTML my $form = HTML::Form->parse($html, "/"); # that would be something like $form = $mech->current_form() in WWW::M +echanize context my $input = $form->find_input("file_name"); $input->headers(content_type => "application/vnd.ms-excel"); my $req = $form->click(); print $req->as_string();

    which would output (with test.csv containing the "foo;bar;..."):

    POST http://localhost/ Content-Length: 219 Content-Type: multipart/form-data; boundary=xYzZY --xYzZY Content-Disposition: form-data; name="file_name"; filename="test.csv" Content-Type: application/vnd.ms-excel foo;bar;baz 1;2;3 --xYzZY Content-Disposition: form-data; name="submit" Submit --xYzZY--

    (commenting out the $input->headers(...), you'd get the default "Content-Type: text/plain" for test.csv instead...)