Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

So, the PERL code to connect to the server and get the devices is as follows:


Code Block
languageperl
#!/bin/perl -w

...


# Perl script to access JSON data from iQSonar REST API

...


use LWP;                            # Module to get web page content

...


use JSON::XS ;                      # Module to parse JSON

...


my $iQSonar = 'vm-mike-2k12a.iqdomain.com:80';  # URL of the REST API for the iQSonar instance, for credentials

...


my $realm = 'iQSonar RestAPI Realm';

...


my $browser = LWP::UserAgent->new;                    # need to get a web page

...


$browser -> credentials($iQSonar,$realm,'admin'=>'password');

...


my $url =  'http://vm-mike-2k12a.iqdomain.com/api/v1/devices';

...


my $DeviceList = $browser→get($url);

...


   die "Error: unable to get REST API results"

...


   unless $DeviceList->is_success;

...


# The variable $DeviceList now contains the JSON output list of known devices

...


my @ParsedDeviceList = @{decode_json( $DeviceList->content ) };

...


            #  Parse into a PERL Array of objects



At this point in the script, we have a variable $DeviceList which contains the JSON output from the Rest API. We then use a function call “decode_json()” from the “JSON::XS” library to convert this text file into a PERL data structure.

...