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

Hello Monks,

This is the first time I'm using LWP, but this script is not producing what I hoped for (I keep getting 'No response'). Can someone take a look at it and tell me if I'm on the right track?
use warnings; use strict; use LWP::Simple; use LWP::UserAgent; my $ua = new LWP::UserAgent; $ua->agent("AgentName/0.1 " . $ua->agent); print "Content-type: text/html\n\n"; # Create a request my $req = new HTTP::Request POST => 'http://cgi.nessus.org/plugins/sea +rch.html'; $req->content_type('application/x-www-form-urlencoded'); $req->content('search=1999-0874&name=cve_id'); # Pass request to the user agent and get a response back my $res = $ua->request($req); # Check the outcome of the response if ($res->is_success) { print $res->content; } else { print "No response\n"; }
Thanks!

Replies are listed 'Best First'.
Re: Help With First LWP Script
by Mercio (Scribe) on Jul 12, 2004 at 16:41 UTC
    This is what I would change it to to get it to work.

    The %param are the variables you are passing to the search.php3 script. search is, a no brainer, what you are looking for and search_query is the types. I got these from looking at the code on there search page.

    Enjoy.
    use warnings; use strict; use LWP::Simple; use LWP::UserAgent; my $ua = new LWP::UserAgent; print "Content-type: text/html\n\n"; # Create a request my %param; $param{'search'} = 'GD'; $param{'search_query'} = 'name'; my $res = $ua->post('http://cgi.nessus.org/plugins/search.php3', \%par +am); # Check the outcome of the response if ($res->is_success) { print $res->content; } else { print "No response\n"; }
Re: Help With First LWP Script
by hsinclai (Deacon) on Jul 12, 2004 at 15:46 UTC
    Say, shouldn't the post go to the search.php3 URL instead of the search.html page?
    <form action="search.php3" method="post">


      When I change it to that, I now get:
      Error - you did not enter a search query
Re: Help With First LWP Script
by tomhukins (Curate) on Jul 12, 2004 at 17:36 UTC

    Other responses should help you solve your problem, but I hope you don't mind me pointing out a couple of minor style issues with your code:

    You use LWP::Simple yet never do anything with this class. If you don't call any methods on a class, you don't need to include it in your program.

    You create an LWP::UserAgent object using an indirect method call (new LWP::UserAgent as opposed to LWP::UserAgent->new()) which will cause problems if you have a new() subroutine in your current package. In general, it's best to avoid indirect method calls.

      Thanks Mercio, that helped me to get it working and thanks to you, tomhukins, I cleaned up my script with your recommendations.