Skip to main content

HTTP::Response may have a different definition of success than you do

·230 words·2 mins·
HTTP perl
❤️ It's great to see you here! I'm currently available on evenings and weekends for consulting and freelance work. Let's chat about how I can help you achieve your goals.

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.


Related

On the status of HTTP::BrowserDetect
·837 words·4 mins
browser detection HTTP perl
Upgrading Business::PayPal::API
·369 words·2 mins
perl
Stop Writing Your Own Commify Functions
·802 words·4 mins
perl