Standard REST methods are supported on the API, which include POST, GET, and DELETE, operations through HTTP.
REST HTTP and HTTPS-Based Methods
Method |
Action |
Behavior |
POST |
Create/Update |
Idempotent |
GET |
Read |
Nullipotent |
DELETE |
Delete |
Idempotent |
The POST and DELETE methods are idempotent, meaning they have no additional effect if they are called more than once with the same input parameters. The GET method is nullipotent, meaning it can be called zero or more times without making any changes (or that it is a read-only operation).
Payload Encapsulation
Payloads to and from the NX-API REST interface can be encoded with either XML or JSON. The XML encoding operation uses the element tag as the name of the package and class, with the respective properties of that object being specified as attributes of the element. Containment is defined by creating child elements.
The following example creates a BGP instance in an XML payload.
Create BGP - XML
JSON encoding requires definition of certain entities to reflect the tree-based hierarchy.
- All objects are described as JSON dictionaries. The key is the name of the package and class while the value is another nested dictionary with two keys: attribute and children.
- The attribute key contains a further nested dictionary describing key-value pairs that define attributes on the object.
- The children key contains a list that defines all the child objects. The children in this list are dictionaries containing any nested objects which are defined as described in the MIT.
The following example creates a BGP instance in a JSON payload.
Create BGP Peer - JSON
Both examples have been abbreviated to simplify visual understanding.
Read Operations
After the object payloads are properly encoded as XML or JSON, they can be used in create, read, update, or delete (CRUD) operations through the REST API.
Read Operations
Response to the above query:
Because NX-API REST uses HTTP, defining the universal resource identifier (URI) to access a certain resource type is important. The first two sections of the request URI simply define the protocol and access details (hostname, IP address) of a Cisco Open NX-OS device. Next in the request URI is the literal string "/api", followed by the DN of the object or class being queried. The final part of the request URI is the encoding format: JSON or XML.
The optional part of a request URI consists of any query options. Query options provide filtering capabilities to developers, and are explained extensively in the NX API REST documentation available at https://developer.cisco.com/site/nx-os.
Response to the above query:
The example above shows a query for all objects with class "l1PhysIf". For a complete reference to the various objects and their properties and possible values, refer to the Cisco NX-API REST documentation at https://developer.cisco.com/site/nx-api/documents/apis/n3k-n9k-api-ref.
Write Operations
Create and update operations in the REST API are implemented using the POST method, so if an object does not already exist, it will be created. If the object already exists, it will be updated to reflect any changes between its existing state and desired state.
Both create and update operations can contain complex object hierarchies. A complete tree can be defined in a single command as long as all objects are within the same context and root, and are under the 1MB limit for the REST API data payloads. This limit is in place to guarantee performance and protect the system under high load. Very large operations may need to be broken into smaller pieces.
Write Operations
Create and update operations use the same syntax as read operations, except they are always executed at an object level, as you cannot make changes to every object of a specific class. The create or update operation should target a specific managed object; the literal string "/mo" in a URL indicates the DN of the managed object will be provided, followed by the DN. Filter strings can be applied to POST operations. As an example, if you want to retrieve the results of your POST operation in the response, you can pass the rsp‑subtree=modified query string to indicate you want the response to include any objects that have been modified by the POST operation.
The payload of the POST operation will contain the XML or JSON encoded data representing the managed object being queried.
In summary, REST-based APIs are the most popular APIs today. They are easy to use, well documented and language independent. NX-API REST exposes these benefits while presenting a comprehensive data model for managing network infrastructures.