Perl REST客户端,身份验证问题 [英] Perl REST client, authentication issue

查看:191
本文介绍了Perl REST客户端,身份验证问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Perl 5.16与 REST::Client 一起用于GET的REST调用,但是显示错误401身份验证问题.我不清楚如何解决此问题.

I am using Perl 5.16 with REST::Client for REST call with GET, but it shows an error 401 authentication issue. I am not clear how to resolve this issue.

代码

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();

推荐答案

您似乎在做 HTTP基本身份验证.为此,您需要具有以下标题:

It looks like you are doing HTTP Basic authentication. For that, you need to have your header as follows:

Authorization: Basic foobar

foobarusername:password的base64表示形式.

Where foobar is the base64 representation of username:password.

在Perl中,它给出:

In Perl, that gives:

my $headers = {
  Accept => 'application/json',  
  Authorization => 'Basic ' . encode_base64($username . ':' . $password)
  #                      ^ note the space here                      
};


请注意, HTTP :: Headers还提供了一种设置HTTP基本身份验证的方法.不幸的是,REST :: Client并没有直接接受它.


Note that HTTP::Headers also provides a method to set HTTP Basic authentication. Unfortunately that's not directly accepted by REST::Client.

这篇关于Perl REST客户端,身份验证问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆