Beefy Boxes and Bandwidth Generously Provided by pair Networks Ovid
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

"Unexpected field value" error with WWW::Mechanize

by bobf (Monsignor)
on Mar 17, 2005 at 08:07 UTC ( #440293=perlquestion: print w/ replies, xml ) Need Help??
bobf has asked for the wisdom of the Perl Monks concerning the following question:

I beseech the monks for wisdom and insight. I'm trying to learn how to use WWW::Mechanize to submit data to a form, but I'm clearly doing something wrong and try as I might I just can't seem to find the error. As a test case I'm using our beloved http://search.cpan.org and searching for 'mechanize'. Here's the code:

use strict; use warnings; use WWW::Mechanize; use HTTP::Cookies; use Data::Dumper; my $url = 'http://search.cpan.org'; my $searchstring = 'mechanize'; my $mech_obj = WWW::Mechanize->new(); $mech_obj->get( $url ); if( not $mech_obj->success() ) { print "Could not retrieve page:\n"; print $mech_obj->content(); die; } print "all forms:\n", Dumper( [ $mech_obj->forms ] ); $mech_obj->form_number( 1 ); $mech_obj->field( 'query' => $searchstring ); print "current form:\n", Dumper( $mech_obj->current_form() ); $mech_obj->submit(); print "content:\n", $mech_obj->content();

And here is the output:

all forms: $VAR1 = [ bless( { 'inputs' => [ bless( { 'value' => '', 'size' => '35', 'type' => 'text', 'name' => 'query' }, 'HTML::Form::TextInput' ), bless( { 'seen' => [ 1, 0, 0, 0 ], 'menu' => [ 'all', 'module', 'dist', 'author' ], 'current' => 0, 'type' => 'option', 'name' => 'mode' }, 'HTML::Form::ListInput' ), bless( { 'value' => 'CPAN Search', 'type' => 'submit' }, 'HTML::Form::SubmitInput' ) ], 'extra_attr' => { 'class' => 'searchbox', 'name' => 'f' }, 'enctype' => 'application/x-www-form-urlencoded', 'method' => 'GET', 'action' => bless( do{\(my $o = 'http://search.cpan +.org/search')}, 'URI::http' ) }, 'HTML::Form' ) ]; current form: $VAR1 = bless( { 'inputs' => [ bless( { 'value' => 'mechanize', 'size' => '35', 'type' => 'text', 'name' => 'query' }, 'HTML::Form::TextInput' ), bless( { 'seen' => [ 1, 0, 0, 0 ], 'menu' => [ 'all', 'module', 'dist', 'author' ], 'current' => 0, 'type' => 'option', 'name' => 'mode' }, 'HTML::Form::ListInput' ), bless( { 'value' => 'CPAN Search', 'type' => 'submit' }, 'HTML::Form::SubmitInput' ) ], 'extra_attr' => { 'class' => 'searchbox', 'name' => 'f' }, 'enctype' => 'application/x-www-form-urlencoded', 'method' => 'GET', 'action' => bless( do{\(my $o = 'http://search.cpan.o +rg/search')}, 'URI::http' ) }, 'HTML::Form' ); Unexpected field value http://search.cpan.org at (eval 5) line 1

Based on the output I know the proper form was selected and the query string was assigned to the proper field. I don't understand why I get an "Unexpected field value" error when the form is submitted, but I know there has to be a simple explanation for this.

Could someone please run my code and see if it works on a different system? That would at least tell me if the problem is in my code or if it is something else (e.g., the WWW::Mech installation, etc).

Thanks in advance!

Comment on "Unexpected field value" error with WWW::Mechanize
Select or Download Code
Re: "Unexpected field value" error with WWW::Mechanize
by jbrugger (Parson) on Mar 17, 2005 at 19:04 UTC
    I had exactly the same problem.
    I solved it like this:
    use strict; use warnings; use WWW::Mechanize; use HTTP::Cookies; use Data::Dumper; my $url = 'http://search.cpan.org'; my $searchstring = 'mechanize'; my $mech_obj = WWW::Mechanize->new(); $mech_obj->get( $url ); if( not $mech_obj->success() ) { print "Could not retrieve page:\n"; print $mech_obj->content(); die; } print "all forms:\n", Dumper( [ $mech_obj->forms ] ); $mech->submit_form( form_number => 1, fields => { query => $searchstring, } ); print "content:\n", $mech_obj->content();
    "We all agree on the necessity of compromise. We just can't agree on when it's necessary to compromise." - Larry Wall.

      Hi - thanks for checking my code. This code worked for you? I copied exactly what you posted (well, I changed $mech->submit_form to $mech_obj->submit_form) and I still got the same error:

      Unexpected field value http://search.cpan.org at (eval 5) line 1

      It sounds like there is something else going on at my end. I'll have to keep digging. Thanks again for checking this out for me.

      Update: I added use Carp 'verbose' to the test script and got this:

      Unexpected field value http://search.cpan.org at C:/ActivePerl/site/li +b/HTTP/Headers.pm line 256 HTTP::Headers::_header('HTTP::Headers=HASH(0x2249820)', 'Referer', 'ht +tp://search.cpan.org') called at C:/ActivePerl/site/lib/HTTP/Headers +.pm line 150 (etc)

      The error is coming from the _header subroutine in HTTP::Headers. The code in question is this:

      if (!ref($val)) { push(@new, $val); } elsif (ref($val) eq 'ARRAY') { push(@new, @$val); } else { Carp::croak("Unexpected field value $val"); }

      I added a Data::Dumper print statement to determine what $val was, and I got the following:

      $VAR1 = bless( do{\(my $o = 'http://search.cpan.org')}, 'URI::http' );
      which matches the "action" value in the $mech_obj above. The _header routine seems to want scalars (or a ref to an array), but it looks like it's getting a hash ref (or is it the whole object?) from the WWW::Mechanize object instead.

      I was not sure how to proceed, so emailed the authors of those modules. Additional suggestions are welcome. (Incidently, now that I know more about the error, I found a previous report of it in HTTP::Message.)

        Hi, that's weird, indeed it works for me...
        Is your WWW::Mechanize properly installed? my script is here
        and the output is here
        "We all agree on the necessity of compromise. We just can't agree on when it's necessary to compromise." - Larry Wall.
        Did you ever find a solution? I am having the exact same problem w/ HTTP::Headers.
      Worked for me. (With the variable name typo fixed.)
Re: "Unexpected field value" error with WWW::Mechanize - SOLVED
by bobf (Monsignor) on Jun 13, 2005 at 19:00 UTC

    I didn't track down the specific cause of this error, but upgrading to WWW::Mech 1.12 (as well as several of its dependencies) solved the problem and the test script now runs without error.

    Many thanks to everyone that replied to this thread and helped test and troubleshoot, as well as davido and planetscape for their help in the cb.

    Output from the test script (error free!):

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (18)
As of 2014-04-23 09:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (541 votes), past polls