The Hidden Value of the DevNet SandBox and API Explorer

0
83

You may have noticed from some of my other posts like this one or that one that I am working on learning python to use with my other networking skills. I have worked with HTML and PHP in the past, but never Python. I have been slowly getting started and not going to lie, I’ve had my issues already.

One of the things Cisco has done with their commitment to DevNet is made an abundance of resources available for people to learn with. One of which is the DevNet Sandbox. Before today, I used the sandbox as a place to test my skills and theories. Now I understand their is even more to it!

My Use Case: FMC Rest API

I have been trying to do some work with the Firepower Management Center API since that is a system I use regularly and want to expand my skills with. The process as I understood it was 1.) Generate an Authorization Token and then 2.) send my GET and POST requests for data as I saw fit. For the life of me I could not get the authentication and POST sections of my code to work.

I took a small break and started looking at the API explorer that was referenced in the FMC Sandbox. I let the API explorer run a few GET requests, then I noticed it:

I could take that API operation that I just ran in the API explorer and export it as Python code. So I did just that. I copied the code over to my CentOS VM that I use for coding and I was able to get things working. By taking advantage of the SandBox and this “sample” code, I found my issue and what I missed comparing my code to this part of the exported code (Spoiler Alert: it was around my headers):

headers = {'Content-Type': 'application/json'}
r = requests.post(auth_url, headers=headers, auth=requests.auth.HTTPBasicAuth(username,password), verify=False)

And then I was not handling the response correctly either, I needed to add this:

auth_headers = r.headers
auth_token = auth_headers.get('X-auth-access-token', default=None)

In terms of the overall script I ran in the API explorer and then exported, it worked after a couple tweaks on my VM. This output was requesting the access policies from FMC:

Wrap-UP

At this point, I have my auth token and a structure I can build off of. A couple hours of troubleshooting was put aside by a simple export from the API explorer in the DevNet Sandbox. If you are starting into the world of programming as a network engineer, I suggest you give it a try. Between the labs and the test environment, it’s a good resource to have available.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.