cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
731
Views
0
Helpful
3
Replies

Postman API connection to DNAC

DAVID
Level 3
Level 3

How can I overcome the DNAC token timing out when using Postman?  I have created an environment within Postman with the DNAC variables like host, username and password. Right now the current token will expire after a set length of time and I have to run another POST to get the new token.  I'm new to Postman and API's but I'm sure there's a better way of using Postman and DANC

3 Replies 3

Ruben Cocheno
Spotlight
Spotlight

@DAVID 

You probably need to iterate the Token retrieve when you run a chain of requests and i'm don't think that you can change the default timer (60 seconds). The script below to retrieve the Token

#!/usr/bin/python3

import requests

import json


from prettytable import PrettyTable


print ("start"); 

# API URL to get a List of Network devices


requests.packages.urllib3.disable_warnings()

url = "https://<IP Address>/dna/intent/api/v1/network-device"


payload = {}


headers = {

    'X-Auth-Token':

    '<Paste Token retrieved via Postman.'

}

response = requests.request("GET", url, verify=False, headers=headers, data = payload)


# Take the response and turn it into Python Objects

raw_output = json.loads(response.text)


# raw_output is a Dictionary. Take the value of response and assign to devices

devices = raw_output["response"]


#Iterate through the list to print out hostnames of devices

for device in devices:

    print("Hostname: {}".format(device["hostname"]))

Tag me to follow up.
Please mark it as Helpful and/or Solution Accepted if that is the case. Thanks for making Engineering easy again.
Connect with me for more on Linkedin https://www.linkedin.com/in/rubencocheno/

So let me see if I have this correct.  The token from DNAC will expire at 60 seconds so any time I need to run a python script or postman API, I will first need to get the new token from DNAC to use it in a python script or postman requests and I'll have to do this ever time I make a new request to DNAC that is outside of the 60 second time limit??

You will typically want to fetch and renew the token using the API using basic auth in your scripts. The token does only last 60 seconds, but If you use the DNA Center SDK or similar it will handle the token renewal automatically. You can read more about this in the "getting started" portion of the API docs: https://developer.cisco.com/docs/dna-center/#!getting-started

For Postman you can chain requests and use data from responses(like the token) in subsequent requests. This way you won't have to rush through fetching the token and executing your requests. The following blog article is a good place to start for this: https://blog.postman.com/extracting-data-from-responses-and-chaining-requests/

Happy to help! Please mark as helpful/solution if applicable.
Get in touch: https://torbjorn.dev

Review Cisco Networking for a $25 gift card