http://www.perlmonks.org?node_id=62042

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

How to submit the nickname and password for the HTML page below?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>Untitled</title> <link rel=stylesheet type="text/css" href="../images/friendsco +ut.css"> </head> <body background="../images/fs_bg/bg_login.gif" bgcolor="#95B8CE" topm +argin="0" leftmargin="0" marginheight="0" marginwidth="0"> <FORM method="get" action="/cgi-bin/pu_help2.fcgi"> <input type="HIDDEN" name="id" value="1-d24-6UAPfv6PUogp1jjKEb60Giw"> <input type=hidden name="page" value="loginp"> <table cellpadding=0 cellspacing=0 border=0> <tr> <td colspan=3><img src="../images/nix.gif" width="1" height="20" b +order="0"> </td> </tr> <tr> <td><img src="../images/nix.gif" width="10" height="1" border="0"> +</td> <td colspan=2>Pseudonym</td> </tr> <tr> <td>&nbsp;</td> <td colspan=2><input type=text name="nickname" size=18 maxlength=4 +0></td> </tr> <tr> <td colspan=3><img src="../images/nix.gif" width="1" height="8" bo +rder="0"></td> </tr> <tr> <td>&nbsp;</td> <td colspan=2>Passwort</td> </tr> <tr> <td>&nbsp;</td> <td><input type=password name="password" size=10 maxlength=30></td +> <td align=right><input type="image" value="Submit" src="../ima +ges/fs_buttons/go.gif" width="26" height="17" border="0" alt="Go!"></ +td> </tr> </table></form> </body> </html>
I tried fiddling with the following Perl code but can't get it to work:
my $req = new HTTP::Request('POST', $url, $hdrs); $req->content_type("application/x-www-form-urlencoded"); $req->content("nickname=JohnSmith&password=test");

Replies are listed 'Best First'.
Re: How to submit name & password using LWP ?
by merlyn (Sage) on Mar 04, 2001 at 00:48 UTC
    You're working too hard. From perldoc HTTP::Request::Common:
    POST $url, [$form_ref], [Header => Value,...] This works mostly like GET() with POST as the method, but this function also takes a second optional array or hash reference parameter ($form_ref). This argu- ment can be used to pass key/value pairs for the form content. By default we will initialize a request using the "application/x-www-form-urlencoded" content type. This means that you can emulate a HTML <form> POSTing like this: POST 'http://www.perl.org/survey.cgi', [ name => 'Gisle Aas', email => 'gisle@aas.no', gender => 'M', born => '1964', perc => '3%', ]; This will create a HTTP::Request object that looks like this: POST http://www.perl.org/survey.cgi Content-Length: 66 Content-Type: application/x-www-form-urlencoded name=Gisle%20Aas&email=gisle%40aas.no&gender=M&born=1964& +perc=3%25

    -- Randal L. Schwartz, Perl hacker

Re: How to submit name & password using LWP ?
by epoptai (Curate) on Mar 04, 2001 at 01:08 UTC
    Check out my recent node Login, post, reload. for a complete working subroutine that uses the technique outlined by merlyn. It also shows how to use the useragent to deal with cookies for multiple requests.
How to tell if POST works?!
by BinBerliner (Novice) on Mar 04, 2001 at 01:27 UTC
    The following snippet of code does the same thing no matter which "nickname" and "password" are entered. How can the client tell if they have been accepted or rejected?
    use LWP::UserAgent; use HTTP::Request::Common; $ua = LWP::UserAgent->new; my ($response, $url) = clickon("", ''); ($response, $url) = clickon($response->content, 'content="\d+;url=([^" +]+)'); ($response, $url) = clickon($response->content, 'location.href="([^"]+ +)"'); ($response, $url) = clickon($response->content, 'frame name="login" sr +c="([^"]+)"'); $response = $ua->request(POST $url, [nickname => "JohnSmith", password + => "test"]); print $response->content; print $response->message; sub clickon { my $content = shift @_; my $filter = shift @_; if ($content =~ /$filter/m) { print "\n\n*********MATCH($1)*******\n\n"; my $url = "http://www.friendscout.de$1"; return ($ua->request(GET $url), $url); } else { die "redirect not found !"; } }
Re: How to submit name & password using LWP ?
by BinBerliner (Novice) on Mar 04, 2001 at 02:05 UTC
    I see the problem now... POST is inappropriate for this web page... the HTML learning curve continues :-)