vADC Docs

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

by ricknelson on ‎04-02-2013 03:31 AM - edited on ‎07-14-2015 03:08 PM by PaulWallace (817 Views)

The following code uses Stingray's RESTful API to a file to the extra directory.   The code is written in Perl.  This program adds the file 'validserialnumbers' to the extra directory and if the file already exists it will be overwrite it.  If this is not the desired behavior, code can be added to to check for the existence of the file as was done in the addpool example.



use REST::Client;

use MIME::Base64;

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


my $fileName = 'validserialnumbers';

my $url = "/api/tm/1.0/config/active/extra/$fileName";

my $validSerialNumbers = <<END;





# Set up the connection

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


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

# For files, the MIME type is octet-stream

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

$client->PUT($url, $validSerialNumbers);

# If the file already exists, it will be replaced with this version and 204 will be returned

# otherwise 201 will be returned.

if ($client->responseCode() == 201 || $client->responseCode() == 204) {

    print "File $fileName added";

} else {

    print "Error adding file $fileName.  Status: " . $client->responseCode() . " URL: $url";



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:



File validserialnumbers added




Since this is a file and not a configuration resource, JSON will not be used and the MIME type will be "application/octet-stream".  Another difference when dealing with files is how Stingray handles adding a file that already exists.  If the file already exists, Stingray will overwrite the it and return a HTTP status code of 204.  If the file doesn't already exist, the HTTP status code will be a 201.


Read More