Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

LWP::UserAgent error responce

by sdyates (Scribe)
on Sep 20, 2002 at 16:37 UTC ( [id://199523]=perlquestion: print w/replies, xml ) Need Help??

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

Well, I am not getting what I want from LWP::UserAgent. Perhaps I am not asking the right question, or perhaps I need to use a different module

I am able to use $res->content to dump the responce of a positive query. However, I cannot get a failed responce to generate any text. Just like using a simple browser, if I get a 404, I want to see this and process it. When I try and process $res->content after a failed responce, I have a null dump!

Any ideas?


Thanks in advance guys
Simon

Replies are listed 'Best First'.
Re: LWP::UserAgent error responce
by Helter (Chaplain) on Sep 20, 2002 at 17:19 UTC
    From the Perl & LWP (O'Reilly) book:
    Check the status of the response:
    $response->is_success() $response->is_error() $response->is_redirect() $response->is_info() (probably not used much...)
    And if it is not a success it has some nice functions to figure out what the problem is:
    $response->code() (numeric code returned, error or not) $response->message(); (text status code) $response->status_line() (both numeric and string error codes)
    Or as above you can get error_as_HTML and use that.
Re: LWP::UserAgent error responce
by Solo (Deacon) on Sep 20, 2002 at 16:48 UTC
    From the HTTP::Response POD:
    $response = $ua->request($request) if ($response->is_success) { print $response->content; } else { print $response->error_as_HTML; }
    --
    May the Source be with you.

    You said you wanted to be around when I made a mistake; well, this could be it, sweetheart.

Re: LWP::UserAgent error responce
by jonjacobmoon (Pilgrim) on Sep 20, 2002 at 16:43 UTC
    Can you show us the code, or at least the crucial part?


    I admit it, I am Paco.
Re: LWP::UserAgent error response
by hacker (Priest) on Sep 21, 2002 at 13:08 UTC
    You could also use LWP::UserAgent and HTTP::Request as follows:
    use strict; use LWP::UserAgent; use HTTP::Request; my $req = HTTP::Request->new(HEAD=>$url); my $ua = LWP::UserAgent->new; my $resp = $ua->request($req); my $status_line = $resp->status_line; my %errors = ('500'=>'Bad hostname supplied', '404'=>'URL not found', '403'=>'URL forbidden', '401'=>'Authorization failed', '400'=>'Bad request found', '302'=>'Redirected URL' ); if ($status_line =~ /200/) { # do stuff } else { ($status_line) = ($status_line =~ /(\d+)/); if (defined($errors{$status_line})) { validate_error($errors{$status_line}); } else { validate_error('Unknown error.'); } $success = 0; } sub validate_error { my ($errors) = @_; $url = "$errors"; }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2024-04-18 02:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found