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

I really want to be figuring out an htaccess Auth Basic / WWW::Mechanize interaction issue involved with submitting a form to get me going on a test suite for an appI'm helping to build. I'm authorized to access the protected content until I $agent->click() and then I lose authorization. I reached for WWW::Mechanize::Shell, thinking it might help. I haven't used this in several months, and now I can't seem to get even it working.

Any clues would be appreciated.

-- Hugh

THIS ISSUE HAS BEEN RESOLVED!

I added the following:

$agent->add_header( Authorization => 'Basic Y2kjsdfgiuj mFzcpnsaXjfguy +siufgybewpV0aA==' );
just prior to the ->submit_form() method's invocation. The actual value assigned to Authorization was harvested from the LiveHTTPHeaders view of the request made through a firefox browser.

It now returns a valid webpage that reflects the interaction. Now I can start testing my database interactions. jonsmith1982: thanks for the eye and the hand. Hope this solution might prove useful to others.

UPDATE:
I've given up on the WWW::Mechanize::Shell piece, unless someone offers a clue. I suspect its an issue of me never using one-liners from the command line and not being sure how to properly feed arguments in.

But the real issue for me here is that after establishing credentials and giving me access to the form, it denies me credentials and gives me a 401 Authorization Denied error when I submit the form.

My errors are currently looking like this:

Submitting NextCustomer Button. Form submission failed: 401 Authorization Required $result is 0.
The code below has also been updated to reflect my work over the last hour or so.

The code which I'm working on looks like this:

#!/usr/bin/perl -w use strict; use Test::More; use WWW::Mechanize; use MIME::Base64; my $agent = WWW::Mechanize->new(); my $url = "http://www.ourdomain.com/path/to/our/script"; my $user = "testy_the_tester"; my $pw = "secret"; my @args = ( Authorization => "Basic " . MIME::Base64::encode( $user . ':' +. $pw ) ); $agent->get( $url , @args ); print "\n\n\n"; print "First invocation of webpage.\n"; print "\n\n\n"; my $response = $agent->content(); # $response = grep /ID #:/, $response; print $response; print "\n\n\n"; print "Submitting NextCustomer Button."; print "\n\n\n"; my %fields = ( 'status' => 'Contacted', 'customer' => 'Customer', 'callback' => 'callback', 'comments' => 'This is a test comment generated by th +e 10data_integrity script.', '_submit_check' => '1', 'NextCustomer' => ' Next ', ); # my $response = '0'; $agent->add_header( Authorization => 'Basic 2kjsdfgiujmFzcpnsaXjfguysi +ufgybewpV0aA==' ); my $response2 = $agent->submit_form( form_name => 'VoterCall', with_fields => \%fields, button => "NextVoter", ); if ($response2->is_success) { if($response2->content =~ /401 Authorization Required/){ print "Form submission failed at authorization stage.\n\n"; } else { print "\nAccess was granted, the results follow: \n\n",$response2- +>content; } } else { print STDERR "Form submission failed: \n", $response2->status_line, +"\n"; } # print "This resulted in the following response:\n\n $response->conte +nt.\n\n\n"; print "\$result is $result.\n\n\n"; 1; sub get_test_cases { return 1; }
My dead-end on the WWW::Mechanize::Shell piece:

hesco@rck-ltsp:~$ perl -MWWW::Mechanize::Shell -eshell Module File::Modified not found. Automatic reloading disabled. Can't call method "request" on an undefined value at /usr/local/share/ +perl/5.8.4/WWW/Mechanize.pm line 430.
line 430 relates to a request uri. When I feed it my url on the command line, like so

hesco@rck-ltsp:~$ perl -MWWW::Mechanize::Shell -eshell http://www.ourd +omain.com/path/to/our/script
I get this instead:

http://www.ourdomain.com/path/to/our/script Module File::Modified not found. Automatic reloading disabled. Couldn't open 'http://www.ourdomain.com/path/to/our/script' : No such +file or directory
if( $lal && $lol ) { $life++; }

Replies are listed 'Best First'.
Re: Losing Credentials, and Stumped by WWW::Mechanize::Shell startup error.
by jonsmith1982 (Beadle) on Nov 14, 2006 at 00:28 UTC
    shouldn't
    my @args = ( Authorization => "Basic ".MIME::Base64::encode($user.':'.$pw ) );
    be
    my %args= ('Authorization'=>"Basic".MIME::Base64::encode($user.':'.$pw +));
      Thanks for the idea, but no, that broke an otherwise working work-around I'm cobbling together. Apparently the protocol likes that space after 'Basic'.

      -- Hugh

      if( $lal && $lol ) { $life++; }
        sorry for the spelling mistake i meant to put a space after basic but i meant the fact your using a key value pair within an array as oppose to a hash.
Re: Losing Credentials, Auth Basic and WWW::Mechanize
by Anonymous Monk on Nov 14, 2006 at 11:22 UTC
    rtfm
    $agent->credentials( 'servername:portnumber', 'realm-name', 'username' => 'password', );