From fdb180a73f97d5a1456d5ac3d0a5a844e22d0ea9 Mon Sep 17 00:00:00 2001 From: franck cuny Date: Thu, 3 Jun 2010 08:34:13 +0200 Subject: http-console: a simple http console to do REST query (like http://github.com/cloudhead/http-console) --- bin/http-console | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 bin/http-console (limited to 'bin') diff --git a/bin/http-console b/bin/http-console new file mode 100644 index 0000000..6f0e1f8 --- /dev/null +++ b/bin/http-console @@ -0,0 +1,46 @@ +use strict; +use warnings; +use 5.010; +use Term::ReadLine; + +use Getopt::Long; +use YAML::Syck; + +my $url = shift; +my $format_mode = 'append'; +my $format = 'json'; + +GetOptions( + 'm=s' => \$format_mode, + 'f=s' => \$format, +); + +package http::net::console; +use MooseX::Net::API; + +package main; + +my ($content, $result); + +my $term = Term::ReadLine->new("http::net::console"); +my $prompt = $url . '> '; +while (defined(my $in = $term->readline($prompt))) { + if ($in =~ /^(GET|PUT|POST|DELETE)\s(.*)$/) { + my $http_console = + http::net::console->new(api_base_url => $url, api_format => $format, api_format_mode => $format_mode); + $http_console->meta->add_net_api_method( + 'anonymous', + method => $1, + path => $2 + ); + ($content, $result) = $http_console->anonymous; + say $result->content; + } + if ($in eq 'show headers') { + if (defined $result) { + say Dump $result->headers; + }else{ + say "no headers to show"; + } + } +} -- cgit 1.4.1 From 584811b5376059011a5c832649ab8283f270cf7a Mon Sep 17 00:00:00 2001 From: franck cuny Date: Thu, 3 Jun 2010 08:57:48 +0200 Subject: post and put --- bin/http-console | 53 ++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 44 insertions(+), 9 deletions(-) (limited to 'bin') diff --git a/bin/http-console b/bin/http-console index 6f0e1f8..1fd4fd3 100644 --- a/bin/http-console +++ b/bin/http-console @@ -1,18 +1,18 @@ use strict; use warnings; use 5.010; -use Term::ReadLine; +use Term::ReadLine; use Getopt::Long; use YAML::Syck; -my $url = shift; +my $url = shift; my $format_mode = 'append'; -my $format = 'json'; +my $format = 'json'; GetOptions( 'm=s' => \$format_mode, - 'f=s' => \$format, + 'f=s' => \$format, ); package http::net::console; @@ -25,9 +25,12 @@ my ($content, $result); my $term = Term::ReadLine->new("http::net::console"); my $prompt = $url . '> '; while (defined(my $in = $term->readline($prompt))) { - if ($in =~ /^(GET|PUT|POST|DELETE)\s(.*)$/) { - my $http_console = - http::net::console->new(api_base_url => $url, api_format => $format, api_format_mode => $format_mode); + if ($in =~ /^(GET|DELETE)\s(.*)$/) { + my $http_console = http::net::console->new( + api_base_url => $url, + api_format => $format, + api_format_mode => $format_mode + ); $http_console->meta->add_net_api_method( 'anonymous', method => $1, @@ -36,11 +39,43 @@ while (defined(my $in = $term->readline($prompt))) { ($content, $result) = $http_console->anonymous; say $result->content; } - if ($in eq 'show headers') { + elsif ($in =~ /^(POST|PUT)\s(.*)(?:\s\-d\s(.*))$/) { + my $method = $1; + my $path = $2; + my $data = $3; + my $http_console = http::net::console->new( + api_base_url => $url, + api_format => $format, + api_format_mode => $format_mode, + ); + $http_console->meta->add_net_api_method( + 'anonymous', + method => $method, + path => $path + ); + $http_console->api_useragent->add_handler( + request_prepare => sub { + my $request = shift; + $request->content($data); + } + ); + ($content, $result) = $http_console->anonymous; + say $result->content; + } + elsif ($in eq 'show headers') { if (defined $result) { say Dump $result->headers; - }else{ + } + else { say "no headers to show"; } } + elsif ($in eq 'show content') { + if (defined $content) { + say Dump $result->content; + } + else { + say "no content to show"; + } + } } -- cgit 1.4.1