Skip to content

Instantly share code, notes, and snippets.

/generator.diff Secret

Created December 5, 2014 22:28
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 anonymous/5877a1fcfff5be7f7048 to your computer and use it in GitHub Desktop.
Save anonymous/5877a1fcfff5be7f7048 to your computer and use it in GitHub Desktop.
diff --git a/lib/Mojolicious/Command/get.pm b/lib/Mojolicious/Command/get.pm
index 114b87a..1f69a83 100644
--- a/lib/Mojolicious/Command/get.pm
+++ b/lib/Mojolicious/Command/get.pm
@@ -4,7 +4,7 @@ use Mojo::Base 'Mojolicious::Command';
use Getopt::Long qw(GetOptionsFromArray :config no_auto_abbrev no_ignore_case);
use Mojo::DOM;
use Mojo::IOLoop;
-use Mojo::JSON qw(encode_json j);
+use Mojo::JSON qw(decode_json encode_json j);
use Mojo::JSON::Pointer;
use Mojo::UserAgent;
use Mojo::Util qw(decode encode);
@@ -16,13 +16,16 @@ has usage => sub { shift->extract_usage };
sub run {
my ($self, @args) = @_;
+ my @content;
GetOptionsFromArray \@args,
'C|charset=s' => \my $charset,
- 'c|content=s' => \(my $content = ''),
+ 'c|content=s' => sub { @content = $_[1] },
+ 'f|form=s' => sub { @content = (form => decode_json($_[1])) },
'H|header=s' => \my @headers,
- 'M|method=s' => \(my $method = 'GET'),
- 'r|redirect' => \my $redirect,
- 'v|verbose' => \my $verbose;
+ 'j|json=s' => sub { @content = (json => decode_json($_[1])) },
+ 'M|method=s' => \(my $method = 'GET'),
+ 'r|redirect' => \my $redirect,
+ 'v|verbose' => \my $verbose;
@args = map { decode 'UTF-8', $_ } @args;
die $self->usage unless my $url = shift @args;
@@ -63,7 +66,7 @@ sub run {
# Switch to verbose for HEAD requests
$verbose = 1 if $method eq 'HEAD';
STDOUT->autoflush(1);
- my $tx = $ua->start($ua->build_tx($method, $url, \%headers, $content));
+ my $tx = $ua->start($ua->build_tx($method, $url, \%headers, @content));
my $err = $tx->error;
warn qq{Problem loading URL "@{[$tx->req->url]}": $err->{message}\n}
if $err && !$err->{code};
@@ -132,6 +135,8 @@ Mojolicious::Command::get - Get command
mojo get -v -r google.com
mojo get -v -H 'Host: mojolicious.org' -H 'Accept: */*' mojolicio.us
mojo get -M POST -c 'trololo' mojolicio.us
+ mojo get -M POST -f '{"foo":"bar"}' mojolicio.us
+ mojo get -M POST -j '{"foo":"bar"}' mojolicio.us
mojo get mojolicio.us 'head > title' text
mojo get mojolicio.us .footer all
mojo get mojolicio.us a attr href
@@ -143,7 +148,11 @@ Mojolicious::Command::get - Get command
-C, --charset <charset> Charset of HTML/XML content, defaults to auto
detection.
-c, --content <content> Content to send with request.
+ -f, --form <JSON> Arguments for "form" content generator in JSON
+ format.
-H, --header <name:value> Additional HTTP header.
+ -j, --json <JSON> Arguments for "json" content generator in JSON
+ format.
-M, --method <method> HTTP method to use, defaults to "GET".
-r, --redirect Follow up to 10 redirects.
-v, --verbose Print request and response headers to STDERR.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment