JSON
NX-API CLI

NX-API CLI XML & JSON

If you are new to programming or trying to understand data structures, NX-API CLI is the perfect starting point. NX-API CLI allows you to take CLI commands that you already know today and wrap them in a RESTful API framework. This part of the lab is going to make use of a show command and configuration command within the NX-API Sandbox to understand the data structure for making the request and what you should expect in the response data structure of an NX-API CLI Request interface, predominantly focusing on the JSON interface.

The NX-API CLI that uses the XML or JSON interfaces format can make use of both XML and JSON data structures respectively. The request and response elements are tabulated below:

Request Element Description
version Specifies the NX-API version
sid Only valid when response message is chunked. In order to retrieve the next chunk of message, the user is expected to send a NX-API request with sid set to the sid in the previous response message.
type Specifies the type of command:
  • cli_show - show commands
  • cli_show_ascii - show commands, output without formatting
  • cli_conf - configuration commands
  • bash - bash commands
chunk Specifies whether to start displaying show command output before the entire command completes
input Input of one or more commands
Show and configure commands should be kept separate due to different message types
output_format The content-type in the HTTP/HTTPS header determines the type of response, XML or JSON
rollback Specifies the rollback options. Only valid for configuration commands, not show commands.
  • stop-on-error — Stops at the first CLI that fails
  • continue-on-error — Ignores and continues with other CLIs
  • rollback-on-error — Performs a rollback to the previous state the system configuration was in
Response Element Description
version Specifies the NX-API version
type Specifies the type of command:
  • cli_show - show commands
  • cli_show_ascii - show commands, output without formatting
  • cli_conf - configuration commands
  • bash - bash commands
sid Session ID of current response. Only valid for chunked output
outputs Tag enclosing all the commands' outputs
output Tag enclosing output of a single command output. In the cli_conf and bash cases, it contains outputs of all the commands
body The body of the response to the command
code The error code of the command execution. Standard HTTP error codes are used here
msg The error message associated with the error code, providing more details of the command execution status

XML

XML stands for eXtensible Markup Language and is a markup language much like HTML. It is considered to be simple and flexible text format that was designed to carry data. In the example below you can see XML syntax where information is wrapped in pre-defined tags correlating back to the NX-API request elements.

Example NX-API CLI XML Interface Data Structure:

    
        <xmp>
        <?xml version="1.0"?>
            <ins_api>
                <version>1.0</version>
                <type>cli_conf</type>
                <chunk>0</chunk>
                <sid>sid</sid>
                <input>router ospf UNDERLAY</input>
                <output_format>xml</output_format>
            </ins_api>
        </xmp>
    

JSON

JSON stands for JavaScript Object Notation and is an independent text format that is considered to be the ideal data-interchange language due to how it can map directly to various programming languages, including Python. JSON correlates to these programming languages as two different types of data structures. The first data structure type would be as a collection of key/value pairs, where in Python, this would correspond to a dictionary object. You will be leveraging this relationship throughout this lab. The second data structure type would be an ordered list of values, corresponding to something such as an array or list. In the example below, JSON syntax uses pre-defined keys to reference information values correlating back to the NX-API request elements.

Example NX-API CLI JSON Interface Data Structure:

    
        {
            "ins_api": {
            "version": "1.0",
            "type": "cli_conf",
            "chunk": "0",
            "sid": "1",
            "input": "router ospf UNDERLAY",
            "output_format": "json"
            }
        }
    

NX-API CLI Requests using JSON


Step 1 - Setup NX-API Sandbox for a Show Command

In the NX-API Sandbox web application perform the following steps:

  1. Select NXAPI-CLI at the Method (this should be the default)
  2. Select json as the Message Format
  3. Select cli_show for the Input Type (this should be the default)
  4. In the "Enter CLI commands here..." pane, enter show version
  5. Click Send to POST the show command

Locate the two panes: Request: and Response: Within each, you will notice the request and response elements and values. The response in particular provides you a data structure of key/pair values that can be easily parsed.

Request:

    
        {
            "ins_api": {
            "version": "1.0",
            "type": "cli_show",
            "chunk": "0",
            "sid": "sid",
            "input": "show version",
            "output_format": "json"
            }
        }
    

Response:

    
        {
            "ins_api": {
              "type": "cli_show",
              "version": "1.0",
              "sid": "eoc",
              "outputs": {
                "output": {
                  "input": "show version",
                  "msg": "Success",
                  "code": "200",
                  "body": {
                    "header_str": "Cisco Nexus Operating System (NX-OS) Software\nTAC support: http://www.cisco.com/tac\nDocuments: http://www.cisco.com/en/US/products/ps9372/tsd_products_support_series_home.html\nCopyright (c) 2002-2024, Cisco Systems, Inc. All rights reserved.\nThe copyrights to certain works contained herein are owned by\nother third parties and are used and distributed under license.\nSome parts of this software are covered under the GNU Public\nLicense. A copy of the license is available at\nhttp://www.gnu.org/licenses/gpl.html.\n\nNexus 9000v is a demo version of the Nexus Operating System\n",
                    "bios_ver_str": "",
                    "kickstart_ver_str": "9.3(13)",
                    "nxos_ver_str": "9.3(13)",
                    "bios_cmpl_time": "",
                    "kick_file_name": "bootflash:///nxos.9.3.13.bin",
                    "nxos_file_name": "bootflash:///nxos.9.3.13.bin",
                    "kick_cmpl_time": "1/31/2024 12:00:00",
                    "nxos_cmpl_time": "1/31/2024 12:00:00",
                    "kick_tmstmp": "12/13/2023 06:06:50",
                    "nxos_tmstmp": "12/13/2023 06:06:50",
                    "chassis_id": "Nexus9000 C9300v Chassis",
                    "cpu_name": "Intel(R) Xeon(R) CPU E5-2680 v3 @ 2.50GHz",
                    "memory": 8159464,
                    "mem_type": "kB",
                    "proc_board_id": "961VDIPK9AU",
                    "host_name": "staging-leaf1",
                    "bootflash_size": 4495360,
                    "kern_uptm_days": 1,
                    "kern_uptm_hrs": 15,
                    "kern_uptm_mins": 23,
                    "kern_uptm_secs": 32,
                    "rr_reason": "Unknown",
                    "rr_sys_ver": "",
                    "rr_service": "",
                    "plugins": "Core Plugin, Ethernet Plugin",
                    "manufacturer": "Cisco Systems, Inc.",
                    "TABLE_package_list": {
                      "ROW_package_list": {
                        "package_id": ""
                      }
                    }
                  }
                }
              }
            }
          }
    


Step 2 - Reset NX-API Sandbox

In the NX-API Sandbox, click the Reset button.


Step 3 - Setup NX-API Sandbox for a Configuration Command

In the NX-API Sandbox web application perform the following steps:

  1. Select NXAPI-CLI at the Method (this should be the default)
  2. Select json as the Message Format
  3. Select cli_conf for the Input Type
  4. Select Error on Action option to Rollback-on-error
  5. This is a new feature introduced in NXOS 9.x. This feature provides three options in the event a configuration error occurs from sending configuration commands via the API. The Error Actions that are available are Stop-on-error, Continue-on-error, and Rollback-on-error.

  6. In the "Enter CLI commands here..." pane, enter hostname N9Kv-L1
  7. Click Send to POST the configuration command

Once again, locate the two panes: Request: and Response: Within each, you will notice the request and response elements and values. This time, the response provides you a data structure of key/pair values that confirm if the POST request to the device was successful.

Request:

    
        {
            "ins_api": {
            "version": "1.0",
            "type": "cli_conf",
            "chunk": "0",
            "sid": "sid",
            "input": "hostname N9Kv-L1",
            "output_format": "json",
            "rollback": "rollback-on-error"
            }
        }
    

Response:

    
        {
            "ins_api": {
            "sid": "eoc",
            "type": "cli_conf",
            "version": "1.0",
            "outputs": {
                "output": {
                "code": "200",
                "msg": "Success",
                "body": {}
                }
            }
            }
        }
    


Continue to the next section to explore the NX-API CLI JSON-RPC API.