01-03-2024 10:39 AM
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
01-03-2024 11:07 AM
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"]))
01-04-2024 01:21 PM
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??
01-04-2024 01:38 PM - edited 01-04-2024 01:44 PM
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/
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide