Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Remote web form post

by c0ldfire (Initiate)
on Aug 20, 2011 at 18:24 UTC ( #921433=perlquestion: print w/ replies, xml ) Need Help??
c0ldfire has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I'm relatively new to perl, I was trying to create a perl script to remote login in a web form and return either sucess or failure. but its not working or i'm missing something, plus it give me an error message: Here is what i wrote:
#!/usr/bin/perl use LWP::UserAgent; use HTTP::Response; use HTTP::Request::Common qw(POST); $ua = LWP::UserAgent->new; $ua->agent("Mozilla 8.0..."); $username = "username"; $password = "password"; my $req = (POST 'http://www.domain.com/login.php', ["Username" => "$username", "Password" => "$password"]); $request = $ua->request($req); $content = $request->content; if ($res->is_success) { print ("success"); exit; } else { print ("failure"); }
this script is not running at all and the error i'm getting is:

Can't call method "is_success" on an undefined value at c:\remotelogin.pl line 24.

Comment on Remote web form post
Download Code
Replies are listed 'Best First'.
Re: Remote web form post
by linuxer (Deacon) on Aug 20, 2011 at 19:00 UTC

    Where do you define and fill $res?

    That variable is never defined or filled and therefore is undefined.

    Maybe you should consider to use strict and warnings.

    Addendum:

    I had another look at your script and think, there are more flaws than the one I previously mentioned.

    • $req should contain a HTTP::Request object; so you should create one.
    • POST 'http://...' is missing the correct operator between POST and the URL string
    • $ua->request() returns a response object; you should give the variable a better fitting name than $request.

      is missing the correct operator between POST and the URL string

      Sure its not :)

      use DDS; use HTTP::Request::Common qw(POST); Dump( POST 'http://example.com', [ qw' username joe password joe ' ] ) +; __END__ $HTTP_Request1 = bless( { _content => 'username=joe&password=joe', _headers => bless( { "content-length" => 25, "content-type" => 'application/x-ww +w-form-urlencoded' }, 'HTTP::Headers' ), _method => 'POST', _uri => \do { my $v = 'http://example.com' } }, 'HTTP::Request' ); bless( $HTTP_Request1->{_uri}, 'URI::http' );

      I agree with the rest of your words

        Ah, thank you for the hint. I tested with HTTP::Request, which I prefer...

        I thought of POST being a bareword and not an exported function from HTTP::Request::Common.

        Well, now I see, I don't like that style... ;-)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://921433]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2015-07-30 11:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (271 votes), past polls