This has bitten me before, so I thought it was worth writing about. This RT ticket explains it better than I can, but let me sum things up here.
Consider this code:
use LWP::UserAgent ();
my $ua = LWP::UserAgent->new;
my $response = LWP::UserAgent->get('http://example.com/foobar');
if ( $response->is_success ) {
...
}
99 times out of 100, this will do what you mean. Occasionally it doesn’t.
What is the definition of success? In this case it means that there’s an HTTP response code in the 200s.
Q: What happens if you’ve gotten a 200 response code in the server headers but (for example) there’s a problem with the response body?
A: is_success
still returns true.
is_success
gives you a technically correct answer, but in this case technically correct isn’t always helpful because something else may have genuinely gone wrong.
Consider what happens in this case where HTML::HeadParser is not installed.
If you want to check for success with confidence, you may want to check the ‘X-Died’ header as well.
use LWP::UserAgent ();
my $ua = LWP::UserAgent->new;
my $response = LWP::UserAgent->get('http://example.com/foobar');
if ( $response->is_success && !$response->header('X-Died') ) {
...
}
That seems like a lot of work, so I’ve proposed a workaround that will at least warn on the fact that the ‘X-Died’ header exists. I don’t know what the right answer is, but I do know that the current behaviour is confusing.