maxjackie maxjackie - 1 year ago 76
Perl Question

How do I use Perl's LWP to log in to a web application?

I would like to write a script to login to a web application and then move to other parts
of the application:

use HTTP::Request::Common qw(POST);
use LWP::UserAgent;
use Data::Dumper;

$ua = LWP::UserAgent->new(keep_alive=>1);

my $req = POST "",
[ user_name => 'username',
user_password => "password",
module => 'Users',
action => 'Authenticate',
return_module => 'Users',
return_action => 'Login',

my $res = $ua->request($req);
print Dumper(\$res);
if ( $res->is_success ) {
print $res->as_string;

When I try this code I am not able to login to the application. The HTTP status code returned is 302 that is found, but with no data.

If I post username/password with all required things then it should return the home page of the application and keep the connection live to move other parts of the application.

Answer Source

You want WWW::Mechanize:

WWW::Mechanize, or Mech for short, is a Perl module for stateful programmatic web browsing, used for automating interaction with websites.

Features include:

  • All HTTP methods
  • High-level hyperlink and HTML form support, without having to parse HTML yourself
  • SSL support
  • Automatic cookies
  • Custom HTTP headers
  • Automatic handling of redirections
  • Proxies
  • HTTP authentication

Mech supports performing a sequence of page fetches including following links and submitting forms. Each fetched page is parsed and its links and forms are extracted. A link or a form can be selected, form fields can be filled and the next page can be fetched. Mech also stores a history of the URLs you've visited, which can be queried and revisited...

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download