![](/img/trans.png)
[英]Better way to proxy an HTTP request using Perl HTTP::Response and LWP?
[英]Pass perl file arguments to LWP HTTP request
這是我處理參數Perl的問題。 無論給Perl文件提供什么參數,我都需要將Perl參數參數傳遞給http請求(Web服務)。
perl wsgrep.pl -name=john -weight -employeeid -cardtype=physical
在wsgrep.pl文件中,我需要將以上參數傳遞給http post params。
像下面一樣
http://example.com/query?name=john&weight&employeeid&cardtype=physical.
我正在為此URL使用LWP軟件包以獲取響應。
有什么好的方法可以做到這一點嗎?
更新:在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;
}
我需要從給定的參數構造后參數部分。
[
'name' => 'john',
'cardtype' => 'physical'
],
通常,要對參數進行url編碼,我將使用以下代碼:
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;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.