Netscaler NITRO API 101: Logging in with Python

UPDATE: 28/09/2011 – I have resolved the issues I was having with the Citrix blogging system and published an updated version of this post there:

Netscaler NITRO API 101: Logging in with Python

I’ll hopefully be an actual Citrix blogger soon, but in order to try this out, I thought I would post here first and see how it goes…..

If you are using one of the SDKs (Java or C#) then creating a NITRO session on the Netscaler is as simple as creating a new “nitro_service” object, choosing your connection method and setting your credentials appropriately. If have supplied valid credentials and your Netscaler is correctly configured then you can continue on your merry way and remain ignorant of what has just happened in the background. However, if you are looking to use another language to make use of the RESTful interface that NITRO provides, then you will need to know more.

The NITRO API Documentation is available from the Netscaler GUI, of course, but that serves as more of a functional specification than a guide to building a successful REST client in your language of choice. So, what exactly is required to take that first step and log in to a Netscaler?

The short answer is: A properly formatted POST request using appropriate credentials – easy, right?

Creating that properly formatted request, from scratch, can be more tricky that it appears. The exact requirements, along with an example, are after the break.

First of all, let’s take a look at some snippets from the API documentation (taken from Citrix NetScaler NITRO Getting Started Guide for REST API):

Request Payload:


That looks straight forward enough, but as always the devil is in the details. First of all, there are a few prerequisites omitted:

  1. The request must include a Content Type header of: application/x-www-form-urlencoded
  2. The “object=” string at the start of the payload is not just descriptive, it is required
  3. The remainder of the payload should be URL encoded

Unless the above criteria are met you are likely to receive an error response (probably “Invalid Post Request”) as you attempt to login.

It is easy to list the requirements, but often times the code is far more useful. Here is a very basic login request example using the popular Python programming language:

#import the necessary libraries
import json
import httplib2
import urllib
#set the headers and the base URL
headers = {'Content-type': 'application/x-www-form-urlencoded'}
url = ''
#contruct the payload with URL encoding
payload = {"object":{"login":{"username":"user","password":"secret"}}}
payload_encoded = urllib.urlencode(payload)
#create a HTTP object, and use it to submit a POST request
http = httplib2.Http()
response, content = http.request(url, 'POST', body=payload_encoded, headers=headers)
#for debug purposes, print out the headers and the content of the response
print json.dumps(response, sort_keys=False, indent=4)
print json.dumps(content, sort_keys=False, indent=4)

The credentials and IP address of the Netscaler will need to be adjusted in the code above to suit your environment, but otherwise running the code above should be sufficient to log into a Netscaler and receive the required Session ID. What to do next with that Session ID will be the subject of the next post.