-
-
Save jhthorsen/8a56a5c136f1fd06ff2b to your computer and use it in GitHub Desktop.
Print curl command from Mojo::UserAgent requests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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