DelphiFAQ Home Search:
General :: Programming :: perl
General questions about perl.

Articles:

This list is sorted by recent document popularity (not total page views).
New documents will first appear at the bottom.
Recommended links on this topic:
Featured Article

How can a perl script automate HTML form transmissions?

Question:

How can a perl script automate HTML form transmissions (GET or POST)?

Answer:

See the two examples. The first one fills out a login form with 2 fields ('username', 'password') and does a GET request.

The second snippet fills out the same form but transmits the data with a POST request.

Keep in mind that the target URL has to be the URL that is specified in the action tag of the HTML form. This is usually different from the URL where the form is presented on.

#-----------------------------

 # GET REQUEST

 #

 use LWP::Simple;
 use URI::URL;
 
 my $url = url('http://login.yahoo.com/');
 $url->query_form(username => 'peter', password => 'secret');
 $content = get($url);  
 
 #-----------------------------

 # POST REQUEST

 #

 use HTTP::Request::Common qw(POST);
 use LWP::UserAgent;
 
 $ua = LWP::UserAgent->new();
 my $req = POST 'http://login.yahoo.com/',
    [ username => 'peter', password => 'secret' ];
 $content = $ua->request($req)->as_string; 
 

Generated 0:01:35 on Aug 15, 2018