Skip to content

Instantly share code, notes, and snippets.

@kraih
Created October 11, 2011 14:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kraih/1278296 to your computer and use it in GitHub Desktop.
Save kraih/1278296 to your computer and use it in GitHub Desktop.
use Mojo::Base -strict;
use Mojo::UserAgent;
my $ua = Mojo::UserAgent->new;
# Random UserAgent header
$ua->on(start => sub {
my ($ua, $tx) = @_;
$tx->req->headers->user_agent(join '',
map { ('a' .. 'z')[rand 26] } 1 .. 10);
});
# Detect Mojolicious
$ua->on(start => sub {
my ($ua, $tx) = @_;
$tx->res->content->on(body => sub {
say 'Yay, Mojolicious!'
if shift->headers->header('Server') =~ /mojolicious/i;
});
});
# Test JSON
$ua->on(start => sub {
my ($ua, $tx) = @_;
$tx->res->on(finish => sub {
my $res = shift;
$res->error('This is no JSON!')
if $res->headers->content_type =~ /json/i && !defined($res->json);
});
});
my $tx = $ua->get($ARGV[0] // 'mojolicio.us');
say $tx->success ? $tx->res->body : ('Error: ', $tx->error);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment