in reply to "Unexpected field value" error with WWW::Mechanize

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.

Replies are listed 'Best First'.
Re^2: "Unexpected field value" error from HTTP::Headers when using WWW::Mechanize
by bobf (Monsignor) on Mar 18, 2005 at 03:08 UTC

    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.
Re^2: "Unexpected field value" error with WWW::Mechanize
by tphyahoo (Vicar) on Mar 18, 2005 at 09:40 UTC
    Worked for me. (With the variable name typo fixed.)