这是处理Perl参数的问题。我需要将Perl参数参数传递给http请求(Webservice),无论Pergu文件的参数是什么。
perl wsgrep.pl -name=john -weight -employeeid -cardtype=physical
在wsgrep.pl文件中,我需要将上述参数传递给http post params。
如下所示,
http://example.com/query?name=john&weight&employeeid&cardtype=physical.
我正在使用LWP Package来获取响应。
有没有什么好方法可以做到这一点?
更新: 在wsgrep.pl里面
my ( %args, %config );
my $ws_url =
"http://example.com/query";
my $browser = LWP::UserAgent->new;
# Currently i have hard-coded the post param arguments. But it should dynamic based on the file arguments.
my $response = $browser->post(
$ws_url,
[
'name' => 'john',
'cardtype' => 'physical'
],
);
if ( $response->is_success ) {
print $response->content;
}
else {
print "Failed to query webservice";
return 0;
}
我需要从给定的参数构造post参数部分。
[
'name' => 'john',
'cardtype' => 'physical'
],
答案 0 :(得分:1)
通常,为了对params进行url-encode,我会使用以下内容:
use URI;
my $url = URI->new('http://example.com/query');
$url->query_form(%params);
say $url;
您的需求更加精细。
use URI qw( );
use URI::Escape qw( uri_escape );
my $url = URI->new('http://example.com/query');
my @escaped_args;
for (@ARGV) {
my ($arg) = /^-(.*)/s
or die("usage");
push @escaped_args,
join '=',
map uri_escape($_),
split /=/, $arg, 2;
}
$url->query(@escaped_args ? join('&', @escaped_args) : undef);
say $url;