Showing results for 
Search instead for 
Did you mean: 

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

The following code uses Stingray's RESTful API to a file to the extra directory.   The code is written in Ruby.  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.




require 'rest_client'

require 'base64'

require 'json'

fileName = 'validserialnumbers'

url = '' + fileName

auth = 'Basic ' + Base64.encode64('admin:admin')

validSerialNumbers = < 'application/octet-stream', :authorization => auth})

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

    # otherwise 201 will be returned.    

    if response.code == 201 || response.code == 204

        puts "File #{fileName} added"


        puts "Bad status code #{response.code} when adding file #{fileName}"


rescue => e

    puts "Error: URL=#{url} Error: #{e.message}"



Running the example


This code was tested with Ruby 1.9.3 and version 1.6.7 of the rest-client module.


Run the Ruby script as follows:


$ addextrafile.rb

File 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


Version history
Revision #:
3 of 3
Last update:
‎01-13-2021 05:11:PM
Updated by:
Labels (1)