Search code examples
perlhttpcpanwww-mechanizelwp

Is there a module which has a unifying interface for Perl HTTP clients?


A couple of times now I've run into the situation where a module which only supports HTTP::Tiny would benefit from allowing the user to provide her own UserAgent module (LWP::UserAgent, WWW::Mechanize::Cached, etc) Does a module exist which can accept a UserAgent object and then uniformly implement GET, POST etc?

A couple of use cases:

1) I want to provide my own UserAgent so that I can use LWP::ConsoleLogger::Easy to trace the HTTP calls (useful for debugging)

2) Sometimes I want to cache requests when debugging, so I want to use WWW::Mechanize::Cached

I realize that the issues above can be solved in other ways, but I think that having one module that supports several HTTP client implementations could solve a few problems nicely.


Solution

  • I think the answer is that such a module doesn't currently exist. Having said that, I did come across https://metacpan.org/pod/HTTP::Tiny::Mech, which wraps a WWW::Mechanize object and makes it return a HashRef rather than a response object. You can use this to provide your own UserAgent to a module which is expecting an HTTP::Tiny object, while allowing you to take advantage of the mechanize features.