perl REST client

Post Reply
yezdi
Posts: 1
Joined: Mon May 01, 2017 9:53 am

perl REST client

Post by yezdi »

Does anyone test perl REST client ? I can run curl successfully, but fails through REST client. Here is my sample code.

#!/usr/local/bin/perl -l

use JSON;
use REST::Client;
use MIME::Base64;

### Skip SSL certificate verification
$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME}=0;

# Set the request parameters
my $host = 'http://<3par storage>:8008';
my $user = '3parid';
my $pwd = 'passwd';

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

my $encoded_auth = encode_base64("$user:$pwd", '');

# Get ALL incidents
$client->GET('/api/v1/volumes',
{'Authorization' => "Basic $encoded_auth",
# {'Authorization' => "Basic n3bHCMfkbtPvs0DmBtfnEa==",
'Accept' => 'application/json'});

print 'Response: ' . $client->responseContent() . "\n";
print 'Response status: ' . $client->responseCode() . "\n";
foreach ( $client->responseHeaders() ) {
print 'Header: ' . $_ . '=' . $client->responseHeader($_) . "\n";
}


Always get "invalid session key". same code works perfectly on Netapp's and EMC

Error message

Response: {"code":6,"desc":"invalid session key"}

Response status: 403

Header: Connection=close

Header: Date=Mon, 01 May 2017 15:55:27 GMT

Header: Server=hp3par-wsapi

Header: Content-Type=application/json

Header: Client-Date=Mon, 01 May 2017 15:55:27 GMT

Header: Client-Peer=<3par ip>:8008

Header: Client-Response-Num=1


Following curl syntax works fine.

curl -k -H "Accept:application/json" -H "Content-Type:application/json" --data-binary '{"user":"3parid","password":"passwd"}' 'https://<3par ip>:8080/api/v1/credentials'

{"key":"0-7c2e5ee3yecccd8x430ec1zd56d5308c-d25c0759"}

curl -k -H "X-HP3PAR-WSAPI759: 0-7c2e5ee3yecccd8x430ec1zd56d5308c-d25c0759" 'https://<3par IP>:8080/api/v1/cpgs'


Was wondering I have to pass the session key instead of username/passwd through REST client. But do not know how.

Appreciated

Yezdi
mdriepp
Posts: 1
Joined: Thu Jun 14, 2018 7:04 am

Re: perl REST client

Post by mdriepp »

Hi,
please try adding the session key in the header

Code: Select all

#!/usr/bin/perl -w

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

my $client = REST::Client->new(host => 'https://<hpe3par-ip>:8080');

my $data;
my $response;
my $sessionkey;
my $parm;

$data = encode_json({"password" => "mypassword", "user" => "myuser"});

# set headers as far as already know
$client->addHeader("Content-Type","application/json");
$client->addHeader("Accept","application/json");

# request a session-key
$client->POST('/api/v1/credentials',$data);
$response=decode_json($client->responseContent());
$sessionkey = ${$response}{"key"};

# add session-key to header
$client->addHeader("X-HP3PAR-WSAPI-SessionKey",$sessionkey);

# build query string
$parm = '?query="volumeWWN EQ 60002AC0000000000000005E0002019A"';

# GET data
$client->GET('/api/v1/vluns'.$parm);
Post Reply