JSON API » Sample clients » Perl

#!/usr/bin/perl -w

# This is a sample client for the Enswitch JSON API.
# It is released into the public domain.
# It comes with absolutely no warranty, and is not supported.

use JSON;
use LWP::UserAgent;

my $base = 'http://enswitch.example.com/api/json/';
my $function = 'access/list';
my $username = 'testuser';
my $password = 'testpass';

my $lwp = LWP::UserAgent->new();
my $reply = $lwp->get( $base . $function . '/?auth_username=' . $username . ';auth_password=' . $password );
$reply->is_success() or die $reply->status_line();

my $json = JSON->new->allow_nonref();
my $output = $json->decode( $reply->decoded_content() );
my $fatal = 0;
foreach my $r ( @{ $output->{ 'responses' } } ) {
	if ( $r->{ 'code' } >= 400 ) {
		print STDERR $r->{ 'message' }, "\n";
		$fatal = 1;
	}
}
$fatal and exit( 1 );

foreach my $d ( @{ $output->{ 'data' } } ) {
	print 'Remote access account id=', $d->{ 'id' }, ', name=', $d->{ 'name' }, ".\n";
}