下面的代码发送GET请求并按预期工作,但当我将其更改为PUT方法时,它返回401.我已使用curl
和POSTMAN尝试了PUT请求,它按预期工作,但我是在Perl中实现它时遇到了麻烦。
#!/usr/bin/perl
{
package DINAgent;
use strict;
use warnings;
use base 'LWP::UserAgent';
sub get_basic_credentials {
return ('uname','pword');
}
}
require 5.010_001;
use strict;
use warnings;
use HTTP::Request;
use Encode qw(encode_utf8);
my $ip = '192.168.54.24';
my $url = "http://".$ip."/restapi/relay/outlets/=4/state/";
my $header = [
'X-CSRF' => 'x',
'Accept' => 'application/json',
'Content-Type' => 'application/x-www-form-urlencoded',
];
my $data = encode_utf8('value=true');
my $req = HTTP::Request->new('GET', $url, $header, $data);
my $ua = DINAgent->new;
$ua->add_handler("request_send", sub { shift->dump; return });
$ua->add_handler("response_done", sub { shift->dump; return });
my $res = $ua->request($req);
有效但如果我将'GET'更改为'PUT'则会返回401。
my $req = HTTP::Request->new('PUT', $url, $header, $data);
我用curl成功完成了这个:
curl -v -X 'PUT' -H 'X-CSRF: x' -H 'Accept: application/json' --data 'value=true' --digest 'http://uname:pword@192.168.54.24/restapi/relay/outlets/=4/state/'
在POSTMAN中也一样......所以我不确定为什么LWP模块没有进行身份验证?