DelphiFAQ Home Search:

How can a perl script automate HTML form transmissions?

 

commentsThis article has not been rated yet. After reading, feel free to leave comments and rate it.

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; 

Content-type: text/html

Comments:

 

 

NEW: Optional: Register   Login
Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option, or post under a registered account.
 

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity.
The owner of this web site reserves the right to delete such material.

photo Add a picture: