Skip to content

Commit

Permalink
add support for piping data to STDIN as well
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed May 27, 2017
1 parent 53d63ae commit 93f9c3d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
3 changes: 2 additions & 1 deletion Changes
@@ -1,6 +1,7 @@

7.32 2017-05-27
7.32 2017-05-28
- Added -f option to get command.
- Improved get command with support for piping data to STDIN.
- Fixed memory leak in Mojo::IOLoop::Client that sometimes prevented the
connect timeout from working correctly for TLS handshakes.

Expand Down
7 changes: 6 additions & 1 deletion lib/Mojolicious/Command/get.pm
Expand Up @@ -15,11 +15,15 @@ has usage => sub { shift->extract_usage };
sub run {
my ($self, @args) = @_;

# Content from STDIN
vec(my $readbits, fileno(STDIN), 1) = 1;
my $content = select($readbits, undef, undef, 0) ? join '', <STDIN> : undef;

my $ua = Mojo::UserAgent->new(ioloop => Mojo::IOLoop->singleton);
my %form;
getopt \@args,
'C|charset=s' => \my $charset,
'c|content=s' => \my $content,
'c|content=s' => \$content,
'f|form=s' => sub { _form(\%form) if $_[1] =~ /^(.+)=(\@?)(.+)$/ },
'H|header=s' => \my @headers,
'i|inactivity-timeout=i' => sub { $ua->inactivity_timeout($_[1]) },
Expand Down Expand Up @@ -141,6 +145,7 @@ Mojolicious::Command::get - Get command
mojo get -M POST -H 'Content-Type: text/trololo' -c 'trololo' perl.org
mojo get -f 'q=Mojolicious' -f 'size=5' https://metacpan.org/search
mojo get -M POST -f 'upload=@some_file.txt' mojolicious.org
mojo get -M PUT mojolicious.org < some_file.txt
mojo get mojolicious.org 'head > title' text
mojo get mojolicious.org .footer all
mojo get mojolicious.org a attr href
Expand Down
4 changes: 4 additions & 0 deletions lib/Mojolicious/Guides/Cookbook.pod
Expand Up @@ -1450,6 +1450,10 @@ The request can be customized as well.
$ mojo get -M POST -c 'Hello!' http://mojolicious.org
$ mojo get -H 'X-Bender: Bite my shiny metal ass!' http://google.com

Pipe data to be sent with the request to C<STDIN>.

$ echo 'Hello World' | mojo get -M PUT http://mojolicious.org

Submit forms as C<application/x-www-form-urlencoded> content.

$ mojo get -M POST -f 'q=Mojo' -f 'size=5' https://metacpan.org/search
Expand Down

0 comments on commit 93f9c3d

Please sign in to comment.