如何在Perl中为HTTP :: Tiny设置自定义标头?

时间:2016-08-21 21:55:24

标签: perl5

我在理解在Perl 5中为HTTP::Tiny设置标头的正确方法时遇到了一些麻烦。到目前为止,我已经看到了哈希,哈希引用以及无数其他看似无法组合的方法的示例。

为请求设置标头的正确方法是什么?在发送请求之前查看请求的简单方法是什么?

以下是一些示例代码:

#!/usr/bin/env perl                                                                                                                                                                             
use 5.12.1;                                                                                                                                                                                     
use HTTP::Tiny;                                                                                                                                                                                 

my $api_key = "::";                                                                                                                                                                             

my %headers = (Authorization => sprintf 'Bearer %s', $api_key);                                                                                                                                            
my $url = "https://api-fxpractice.oanda.com/v3/accounts";                                                                                                                                          

my $response = HTTP::Tiny->new($url, 
   default_headers => {'Content-Type' => 'application/json'});                                                              

my $response = HTTP::Tiny->new->get($url, { default_headers => \%headers } );                                                                                                                    

print "$response->{status} $response->{reason}\n";                                                                                                                                                                                                
while ( my ( $k, $v ) = each %{ $response->{headers} } ) { 
        print "$k: $_\n";   
    }                                                                                                                                                                                           
}

print $response->{content} if length $response->{content};  

它给了我一个401。

谢谢!

2 个答案:

答案 0 :(得分:0)

事实证明这个问题与我很愚蠢并且没有注意细节有很大关系。基本上,

  1. 我使用真钱api,而不是假货
  2. 我没有正确使用hashref
  3. 我设置'default_headers'而不是'headers'
  4. `

    my $api_key = "::"
    
    my %headers = (
        "Content-Type" => "application/json",
        "Authorization" => sprintf 'Bearer %s', $api_key);
    
    my $url = "https://api-fxpractice.oanda.com/v1/accounts";
    
    my $response = HTTP::Tiny->new->get($url, { headers => \%headers } );
    
    print "$response->{status} $response->{reason}\n";
    
    while ( my ( $k, $v ) = each %{ $response->{headers} } ) {
        for ( ref $v eq 'ARRAY' ? @$v : $v ) {
            print "$k: $_\n";
        }
    }
    
    print $response->{content} if length $response->{content};
    

    `

答案 1 :(得分:0)

哈希表示%hash=(key=>value)

哈希引用仅表示$hashref={key=>value},等于$hashref=\%hash;

所以

$http = HTTP::Tiny->new( %attributes )只是

$http = HTTP::Tiny->new( attr1=>value1, ... )

$response = $http->get($url, \%options)

$response = $http->get($url, {attr1=>value1, ...} )

说明性示例:

use HTTP::Tiny;
HTTP::Tiny->new->get($url);
HTTP::Tiny->new->get($url, { headers => { header1=>value1, header2=>value2 } };

# with headers set in one place
$handle=HTTP::Tiny->new( default_headers=>{h1=>3, h2=>4} );
$handle->get($url);
$handle->get($url, headers=>{ h2=>'overwrite' });

# without shorthand
HTTP::Tiny->new->request('get', $url);
HTTP::Tiny->new->request('get',$url, { headers => { header1=>value1, header2=>value2 } };

# post
HTTP::Tiny->new->request('post',$url, { headers => { header1=>value1, header2=>value2 }, content=>'body to post' };