6

As far as I can read from the WWW::Mechanize documentation, you can do the following to submit a file from a string:

$mech->submit_form(
    fields => {
        'UploadedFile' => [[ undef, 'test2.txt', Content => $content ], 1],
    }
);

This should submit a file with name text2.txt, containing the text in $content (in this case, 'The file is a lie.').

The request failed with an internal server error, however, so I examined the request that was sent, and found this:

--xYzZY
Content-Disposition: form-data; name="UploadedFile"; filename="ARRAY(0x9567570)"

The file is a lie.
--xYzZY

That is clearly not the filename I specified, so I wonder: Am I doing something wrong, or is the module bugged?

Sebastian Paaske Tørholm
  • 49,493
  • 11
  • 100
  • 118
  • I did a test and got `filename="ARRAY(0x1f124d8)"` too, dumping `$mech->res` ( `WWW::Mechanize` 1.66 ). – Marco De Lellis Oct 02 '11 at 19:38
  • Does a key of "with_fields" instead of "fields" yield a different result? "with_fields" would help if there is more than one form and no form has been selected. – Richard Simões Oct 02 '11 at 20:16
  • `with_fields` gave the same result as `fields`. – Sebastian Paaske Tørholm Oct 03 '11 at 06:37
  • That looks really similar to the LWP base field submission (c/f http://lwp.interglacial.com/ch05_07.htm). Does submitting `fields => {'UploadedFile' => [ undef, 'test2.txt', Content => $content ], upload_field => 1 }`, where upload_field is the name of the form submission field, work correctly? – Oesor Oct 03 '11 at 16:30
  • @Oesor: I'm unsure as to what you want me to write instead of `upload_field`? – Sebastian Paaske Tørholm Oct 04 '11 at 09:08

1 Answers1

2

This is a bug in HTML::Form. I have reported it to the author.

In the mean time, if you have HTML::Form version 6.00, you can fix things temporarily by commenting out line 1442 in HTML/Form.pm which reads

$old = $self->file unless defined $old;
Borodin
  • 126,100
  • 9
  • 70
  • 144