Examples and Use-Cases
This chapter provides examples and use-cases for the REST API.
Typical Usage
ATTENTION
The following examples are developed to work with version 8.3 of the Traffic Manager REST API. Ivanti makes no warranty as to their suitability for older versions.
The following code samples demonstrate how to interact with the REST API for a variety of purposes. The examples are based on Perl using the “REST::Client” module to handle the connections to the Traffic Manager REST daemon.
For further information on REST::Client, see the CPAN Web site: www.cpan.org.
A typical Perl client connection might resemble the following:
#!/usr/bin/perl
use REST::Client;
use strict;
# Set up the connection
my $client = REST::Client->new();
$client->setHost( 'https://myhost:9070' );
$client->addHeader( 'Authorization', 'Basic YWRtaW46am9iYmll' );
$client->addHeader( 'Content-Type', 'application/json' );
# Perform a HTTP GET on this URI
$client->GET( '/api/tm/8.3/config/active' );
# Print out the response body
print $client->responseContent();
In the above example, a new connection is established to the REST service on the Traffic Manager "myhost" on port 9070.
The setHost() function allows us to set up a definitive hostname and port to which all requests are made. This is an optional feature, and the full hostname can be supplied when making the actual request if multiple hosts are required.
Two HTTP headers can be added here, one to provide Basic Auth authentication and the other to provide a declaration of the Content Type when making PUT requests. In the majority of cases, the content type is "application/json", apart from transactions involving raw files where it is necessary to use "application/octet-stream".
A GET request is sent to the REST service with a target of the resource URI as the supplied argument. Typically, the above script outputs a JSON structure showing the Traffic Manager resource tree at the top level:
{
"children": [{
"name": "rules",
"href": "/api/tm/8.3/config/active/rules/"
}, {
"name": "actions",
"href": "/api/tm/8.3/config/active/actions/"
},
...
(truncated)
...
{
"name": "auth",
"href": "/api/tm/8.3/config/active/auth/"
}]
}
Each of the following examples make use of a further Perl module "JSON" in order to encode and decode between the JSON string used by REST::Client and a native Perl structure. This is done to simplify the parsing algorithm within the script. Further information regarding the JSON Perl module can be found at the CPAN Web site: www.cpan.org.