Skip to content

Commit

Permalink
add form support to get command
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jan 27, 2015
1 parent ec17273 commit 23d9456
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions lib/Mojolicious/Command/get.pm
Expand Up @@ -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::Util qw(decode encode);
use Scalar::Util 'weaken';
Expand All @@ -15,13 +15,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;
Expand Down Expand Up @@ -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};
Expand Down Expand Up @@ -134,6 +137,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 -H 'Content-Type: text/trololo' -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
Expand All @@ -145,7 +150,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
Expand Down

0 comments on commit 23d9456

Please sign in to comment.