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

I'm trying to get a script working that takes a website in the QUERY_STRING and gets it using LWP, then print the results. Here's what I have, but it doesn't work. It works fine when I run it on my Win32 machine though (with some changes to accomadate the differences). I know it's not secure, and I also know I should be using CGI, but other than that, some help please?
#!/usr/bin/perl -w use strict; print "Content-type: text/html\n\n"; use LWP::UserAgent; use lib "/pathtomodules"; my $ua = new LWP::UserAgent; $ENV{QUERY_STRING} =~ s/%([\da-fA-F][\dA-Fa-f])/pack("C", hex($1))/eg; my $url = $ENV{QUERY_STRING}; my $request = new HTTP::Request $ENV{REQUEST_METHOD} => "$url"; my $response = $ua->request($request); my $content; if ($response->is_success) { $content = $response->content; print $content; }

Replies are listed 'Best First'.
Re: Help with LWP
by Fastolfe (Vicar) on Jan 15, 2001 at 08:44 UTC
    Define "doesn't work". To quote everyone's favorite infobot:

    Look buddy, doesn't work is a strong statement. Does it sit on the couch all day? Is it making faces at you? Does it want more money? Please be specific!

    Basically, what is it doing? Are you getting an error message? If you're just getting a Server Error, examine the error logs. Check the $response for an error condition and have it explain what went wrong. In other words, do some very basic debugging.

Re: Help with LWP
by extremely (Priest) on Jan 15, 2001 at 08:56 UTC
    One, ignoreing for now the CGI issue, (*tch tch* =) you should just use 'GET' in the Request setup, my $request = new HTTP::Request GET => $ENV{QUERY_STRING}; unless you really have a GOOD reason for passing on the type of request that hit your CGI. Also, you don't need to make $url if you are going to modify the ENV directly, either copy the Query String and then modify it or don't bother.

    Two, I'd just use print($response->content) if $response->is_success; Or if you actually want to see errors when they happen you might try:

    if ($response->is_success) { print $response->content; } else { print $response->error_as_HTML; }

    Three, you said the script works on WinXX? How does it fail then? Does it return nothing, garbage, mangled data, what? You have to give us something to go on so we can do more than nitpick your syntax.

    --
    $you = new YOU;
    honk() if $you->love(perl)

Re: Help with LWP
by dkubb (Deacon) on Jan 15, 2001 at 11:13 UTC
    Here's an LWP snippet that does what I think you are trying to do:
    #!/usr/bin/perl -w use strict; use CGI; use HTTP::Request::Common qw(GET POST); use LWP::UserAgent; my $cgi = CGI->new; my $ua = LWP::UserAgent->new; my $response = $ua->request( $cgi->request_method eq 'POST' ? POST $cgi->param('url'), [ $cgi->Vars ] : GET $cgi->param('url') . '?' . $cgi->query_string, ); print $cgi->header, $response->is_success ? $response->content : $response->error_as_HTML;
    There are a few things about this code:
    • It uses CGI.pm, which takes care of all the parameter parsing issues for you. It's generally better, and simpler than using home grown routines.
    • $cgi->Vars returns a hash. If you have parameters that are passed in that could have multiple values, then this could cause you problems. Although, if your parameter names are unique, this should be no problem.
    • You need to pass in a parameter called 'url', which needs to be in the format:
      http://sitename.com/path/to/file.html
    • Will work with only POST and GET requests