JSON API » Sample clients » Javascript

#!/usr/bin/env nodejs

// 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.

var base = 'http://enswitch.example.com/api/json/';
var func = 'access/list';
var username = 'testuser';
var password = 'testpass';

var http = require( 'http' );
http.get( base + func + '/?auth_username=' + username + ';auth_password=' + password, function( reply ) {
	var json = '';

	reply.on( 'data', function( chunk ) {
		json += chunk;
	} );

	reply.on( 'end', function() {
		var output = JSON.parse( json );
		var fatal = 0;
		var responses = output.responses;
		for( var i = 0; i < responses.length; i++ ) {
			if ( parseInt( responses[ i ].code ) >= 400 ) {
				console.log( responses[ i ].message );
				fatal = 1;
			}
		}
		if ( fatal ) {
			return;
		}

		var data = output.data;
		for( var i = 0; i < data.length; i++ ) {
			console.log( 'Remote access account id=' + data[ i ].id + ', name=' + data[ i ].name );
		}
	} );

} ).on( 'error', function( error ) {
	console.log( error.message );
} );