2017-04-20 58 views
0

我是REST :: Client模塊的新手。 我做了一個腳本,將通過上述模塊調用API。下面REST ::客戶端給出錯誤500不是SCALAR引用

是我的腳本:

use REST::Client; 
use Data::Dumper; 

my $client = REST::Client->new(); 

my $resp = $client->request(
    'POST', 
    "https://api.icims.com/customers/{ID}/search/people", 
    { 
     'Connection' => 'close', 
     'Link'  => 'https://api.icims.com/customers/{ID}/people/;rel="person"; 
    title="Person Profile"', 
     'Content-Length' => '280', 
     'Content-Type'  => 'application/json', 
     'Content-Encoding' => 'gzip', 
     'Host'    => 'api.icims.com', 
     'User-Agent'  => 'Apache-HttpClient/4.2.1 (java 1.5)', 
     'Accept-Encoding' => 'gzip,deflate', 
     Accept    => 'application/json' 
    } 
); 

print Dumper($resp->{_res}->{_content}); 

,這是印刷如下回應:

$VAR1 = '500 Not a SCALAR reference'; 

請建議。

+0

你把'{ID}'放在那裏,還是在你的程序中有一個真實的ID?如果是這樣,它應該如何知道ID? – simbabque

+0

'Accept =>'application/json''應該是''Accept'=>'application/json''? –

+1

@simbabque我的錯誤,謝謝 –

回答

0

您錯過了正文您的請求。 A POST request with REST::Client應該有身體第一,然後可選標題。

POST($網址,[$ body_content,%$頭])

瓶坯HTTP POST將所指定的資源。採用自定義請求標頭的可選正文內容和hashref。

由於您似乎沒有身體,只需使用undef即可。

my $resp = $client->request(
    'POST', 
    "https://api.icims.com/customers/{ID}/search/people", 
    undef,            # here 
    { 
     'Connection' => 'close', 
     'Link'  => 'https://api.icims.com/customers/{ID}/people/;rel="person";title="Person Profile"', 
     'Content-Length' => '280', 
     'Content-Type'  => 'application/json', 
     'Content-Encoding' => 'gzip', 
     'Host'    => 'api.icims.com', 
     'User-Agent'  => 'Apache-HttpClient/4.2.1 (java 1.5)', 
     'Accept-Encoding' => 'gzip,deflate', 
     Accept    => 'application/json' 
    } 
); 

這可能是更好的使用POST方法。它更容易閱讀。

my $resp = $client->POST(
    "https://api.icims.com/customers/ID/search/people", 
    undef, 
    { ... } 
); 
+0

感謝您的回覆。 我試着用GET方法也不需要任何機構通過。 我仍然得到同樣的錯誤。 – Aj06

+0

@ Aj06它適合我。它顯然給了我一個錯誤的回答,因爲ID是錯誤的,但是你描述的錯誤沒有發生。 – simbabque

+0

感謝您的幫助 – Aj06