Skip to content

Instantly share code, notes, and snippets.

@jhthorsen
Created July 6, 2014 07:21
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 jhthorsen/8a56a5c136f1fd06ff2b to your computer and use it in GitHub Desktop.
Save jhthorsen/8a56a5c136f1fd06ff2b to your computer and use it in GitHub Desktop.
Print curl command from Mojo::UserAgent requests
use Mojo::Base -base;
use Mojo::UserAgent;
my $ua = Mojo::UserAgent->new;
$ua->on(start => sub {
my ($ua, $tx) = @_;
my $h = $tx->req->headers->to_hash;
my @curl = ('curl');
for my $k (keys %$h) {
next if $k =~ /User-Agent/i;
push @curl, "-H" => qq("$k: $h->{$k}");
}
if (my $body = $tx->req->body) {
$body =~ s!\n!\\n!g;
push @curl, "-d" => qq("$body");
}
say join ' ', @curl, $tx->req->url->to_abs;
});
$ua->get("http://example.com");
$ua->post("http://example.com", "too cool for school");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment