Showing results for 
Search instead for 
Did you mean: 

Tech Tip: Using the RESTful Control API with Perl - addpool

The following code uses Stingray's RESTful API to add a pool.   The code is written in Perl. This program creates a new pool, "pltest", first doing a GET to make sure the pool doesn't already exist, and if the pool doesn't exist, the pool is created by doing a PUT with just the minimum data needed to create a pool.  In this case the program creates a properties hash with just one node.  All other values will get default values when Stingray creates the pool.



use REST::Client;

use MIME::Base64;

use JSON;

# Since Stingray is using a self-signed certificate we don't need to verify it


my $poolName = 'pltest';

my %pool = (properties => {basic => {nodes => [ '']}});

my $url = "/api/tm/1.0/config/active/pools/$poolName";

# Set up the connection

my $client = REST::Client->new();


$client->addHeader("Authorization", "Basic " . encode_base64("admin:admin"));

#First see if the pool already exists


if ($client->responseCode == 404) {

    $client->addHeader("Content-Type", "application/json");

    $client->PUT($url, encode_json(\%pool));

    my $poolConfig = decode_json $client->responseContent();

    if ($client->responseCode() == 201) { # When creating a new resource we expect to get a 201

        print "Pool $poolName added";

    } else {

        print "Error adding pool. status=" . $client->responseCode() . " Id=" . $vsConfig->{error_id} . ": " . $vsConfig->{error_text} . "\n";


} else {

    if ($client->responseCode() == 200) {

        print "Pool $poolName already exists";

    } else {

        print "Error getting pool config. status=" . $client->responseCode() . " Id=" . $vsConfig->{error_id} . ": " . $vsConfig->{error_text} . "\n";




Running the example


This code was tested with Perl 5.14.2 and version 249 of the REST::Client module.


Run the Perl script as follows:



Pool pltest added




The only difference between doing a PUT to change a resource and a PUT to add a resource is the HTTP status code returned.  When changing a resource 200 is the expected status code and when adding a resource, 201 is the expected status code.


Read More



Version history
Revision #:
2 of 2
Last update:
‎01-06-2021 04:06:PM
Updated by:
Labels (1)