Friday, June 30, 2017

Mojo::UserAgent is Best User Agent

For the longest time, I used LWP::UserAgent, because it was always there and it was reliable and there were tons of examples on the internet and almost every other Perl programmer I'd interacted with had used it before. So it was just easier to use it.

But then my requirements for a HTTP client changed.

A few years ago when I started tackling a lot of problems with concurrent/non-blocking solutions, I needed a HTTP client. Initially, because I was using AnyEvent, I just used AnyEvent::HTTP. And then I came across a really annoying issue: all headers are lower-cased and then the first letter is upper-cased. For the particular problem I was solving, this wasn't gonna work.

The next step was to combine AnyEvent::Handle with HTTP::Request and HTTP::Parser::XS. But that quickly introduced new problems; handling 302 responses, keep-alive, chunked responses and everything in between. I also had to package responses into robust objects myself. This was way more work than it was worth.

Finally, I ended up at Mojo::UserAgent. Because it has a blocking interface, it works great as a replacement for LWP::UserAgent. And because everything in Mojolicious was built to be non-blocking and because Mojo supports EV, it ties in well with all of the other libraries I use for my non-blocking code and it replaces my hand-rolled AnyEvent/HTTP::Request/HTTP::Parser::XS approach.

And for all of this, I only need to remember one API and manage one dependency.