我想创建一个HTTPS PUT请求,但是它给了我以下错误:
Error :
Undefined subroutine &HTTP::Request called at test.pl line 15.
这是我的代码:
#!/usr/bin/perl
use lib "/usr/cisco/packages/perl/perl-5.16.3/lib/5.16.3";
use strict;
use warnings;
use LWP::UserAgent;
use HTTP::Request;
use JSON;
use Data::Dumper;
use MIME::Base64;
my $url = 'https://example.com:8443/cli/agentCLI';
my $credentials = encode_base64('username:password');
my $ua = LWP::UserAgent->new(ssl_opts =>{ verify_hostname => 0});
my $req = HTTP::Request->("PUT",$url, 'Authorization' =>" Basic $credentials");
my $response = $ua->request($req);
die 'http status: ' . $response->code . ' ' . $response->message
unless ($response->is_success);
my $json_obj = JSON->new->utf8->decode($response->content);
my $output=Dumper($json_obj);
print $output;
答案 0 :(得分:0)
该例程不存在,您需要添加new关键字来实例化对象请求
my $req = HTTP::Request->new("PUT",$url, 'Authorization' =>" Basic $credentials");
另一种选择,而不是自己手动放置标题是正确使用请求
编辑其他身份验证选项
$req = HTTP::Request->new("PUT", $url);
$req->authorization_basic('username', 'password');
my $response = $ua->request($req);