Subject: | "Unexpected field value" error |
Date: | Fri, 18 Mar 2005 00:16:18 -0600 |
From: | "Freimuth,Robert" <rfreimut [...] im.wustl.edu> |
To: | <bug-WWW-Mechanize [...] rt.cpan.org>, <gisle [...] ActiveState.com> |
Hi,
I am trying to use WWW::Mechanize to log onto a website, but I keep getting
an "Unexpected field value" error. Since the site in question needs cookies
and https, I decided to try to simplify the test case by using
search.cpan.org instead. I kept getting the error and added Carp 'verbose'
to aid debugging. I found a couple other posts (via Google) to various
forums about this same problem, but apparently none were resolved. As I
explain below, I think I tracked it back to a HTTP::Headers method that
receives an object instead of a scalar from WWW::Mech. Here is the code I
used, followed by the output:
********************************************
use strict;
use warnings;
use WWW::Mechanize;
use HTTP::Cookies;
use Carp 'verbose';
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();
********************************************
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.org/search')}, 'URI::http' )
}, 'HTML::Form' );
Unexpected field value http://search.cpan.org at
C:/ActivePerl/site/lib/HTTP/Headers.pm line 256
HTTP::Headers::_header('HTTP::Headers=HASH(0x2249820)', 'Referer',
'http://search.cpan.org')
called at C:/ActivePerl/site/lib/HTTP/Headers.pm line 150
HTTP::Headers::header('HTTP::Headers=HASH(0x2249820)') called at
(eval 5) line 1
HTTP::Message::__ANON__('HTTP::Request=HASH(0x22536c8)', 'Referer',
'http://search.cpan.org'
) called at C:/ActivePerl/site/lib/WWW/Mechanize.pm line 1027
WWW::Mechanize::request('WWW::Mechanize=HASH(0x1835238)',
'HTTP::Request=HASH(0x22536c8)') c
alled at C:/ActivePerl/site/lib/WWW/Mechanize.pm line 533
WWW::Mechanize::submit('WWW::Mechanize=HASH(0x1835238)') called at
C:\test_www_mechanize.pl line 36
********************************************
It looks like the problem occurs when the form is submitted. The code at
HTTP::Headers line 256 is in sub _header:
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 an array of scalars), but it's getting a hash from
the WWW::Mechanize object.
I am using ActiveState perl:
This is perl, v5.6.1 built for MSWin32-x86-multi-thread
(with 1 registered patch, see perl -V for more detail)
Copyright 1987-2001, Larry Wall
Binary build 633 provided by ActiveState Corp. http://www.ActiveState.com
Built 21:33:05 Jun 17 2002
and I have WWW::Mechanize version 0.48. I am running Windows XP.
I would really appreciate any advice you can give me on how to get this
working.
Thank you very much.
Sincerely,
Robert Freimuth, PhD
Post-Doctoral Fellow
Washington University School of Medicine
Department of Internal Medicine
Division of Molecular Oncology
CSRB NT 1021
Campus Box 8069
660 S. Euclid Ave.
St. Louis, MO 63110
Phone: 314-747-5184
FAX: 314-362-3764