我使用带有REST::Client
的Perl 5.16进行GET REST调用,但它显示错误401身份验证问题。我不清楚如何解决这个问题。
代码
use REST::Client;
use JSON;
use Data::Dumper;
use MIME::Base64;
my $username = 'test';
my $password = 'test';
my $client = REST::Client->new();
$client->setHost('http://myurl');
my $headers = {
Accept => 'application/json',
Authorization => 'Full' . encode_base64($username . ':' . $password)
};
$client->GET('folder/file', $headers);
print $client->responseCode();
print $client->responseContent();
答案 0 :(得分:5)
看起来你在做HTTP Basic authentication。为此,您需要具有如下标题:
Authorization: Basic foobar
其中foobar
是username:password
的base64表示。
在Perl中,它给出了:
my $headers = {
Accept => 'application/json',
Authorization => 'Basic ' . encode_base64($username . ':' . $password)
# ^ note the space here
};
请注意HTTP::Headers also provides a method to set HTTP Basic authentication。不幸的是,REST :: Client没有直接接受。
答案 1 :(得分:0)
我遇到了类似的问题,发现我不得不告诉encode_base64不要破坏响应字符串,例如encode_base64($ username。':'。$ password,“”)