HPE Storage Users Group

A Storage Administrator Community




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: perl REST client
PostPosted: Mon May 01, 2017 11:18 am 

Joined: Mon May 01, 2017 9:53 am
Posts: 1
Does anyone test perl REST client ? I can run curl successfully, but fails through REST client. Here is my sample code.

Quote:
#!/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

Quote:
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.

Quote:
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


Top
 Profile  
Reply with quote  
 Post subject: Re: perl REST client
PostPosted: Thu Jun 14, 2018 7:08 am 

Joined: Thu Jun 14, 2018 7:04 am
Posts: 1
Hi,
please try adding the session key in the header
Code:
#!/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);


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 


Who is online

Users browsing this forum: No registered users and 25 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group | DVGFX2 by: Matt