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

Hi Experts, Hi Experts, need help to create perl script to login to one url using perl script and again i have to use that url to pass the userid for further actions. Please help me with the script to login into the test url with username and password and again i have to use that url Ex:
sub remove{ my ($user) = shift; my $ua = new LWP::UserAgent; $ua->agent('Mozilla/4.5'); ## HERE I HAVE TO LOGIN INTO http://test.com/ THIS url using u +sername:test and password:test my $url_arg = ''; if ($ENV=~ /^Dev/i) { $url_arg = "http://test.com/test?USERID=$user"; } my $url = URI::URL->new($url_arg); my $req = HTTP::Request->new ('GET',$url); $res = $ua->request($req); if ($res->is_success) { return $res; }else { print "Content-type:text/html \n\n"; print $res->error_as_HTML; } exit 0; }

Replies are listed 'Best First'.
Re: Perl script to login into one url and again need to use the url
by Corion (Patriarch) on Aug 23, 2017 at 10:42 UTC

    I think you will likely have more success using WWW::Mechanize, as it bundles usual browser behaviour in a more compact way than using LWP::UserAgent directly.

    For web automation, you will have to understand how HTML and HTTP interact. Logging into a website is usually done through a HTML form. You submit that form with the needed values. WWW::Mechanize has the ->submit_form method for that.

      Hello again jitender,

      I agree with fellow monk Corion.I do not have too much experience on this area but I did not proposed that earlier because I thought you want to use strictly the LWP::UserAgent module. Any way, here is a working example that does exactly what you want Perl Script to login to a secure WebSite by using the WWW::Mechanize module.

      Hope this helps, BR.

      Seeking for Perl wisdom...on the process of learning...not there...yet!
Re: Perl script to login into one url and again need to use the url
by thanos1983 (Parson) on Aug 23, 2017 at 10:19 UTC

    Hello jitender

    Welcome to the Monastery. Please use the <code> tags on you code. It is really difficult to review your code when it is not formatted.

    Update: I think all you need is to follow the recommendations of this tutorial LWP::UserAgent and Basic Authentication.

    Looking forward to your update, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!