Security Concepts · Topic 7 of 7

Interpreting Python Scripts That Call Security APIs

Learn to read Python scripts that call security appliance REST APIs: authentication, tokens, headers, JSON, status codes, pagination and error handling.

  • 4 min
In a nutshell
  • Authenticate first, then send the token with every later request.
  • On the FMC, the token comes back in the X-auth-access-token response header.
  • GET reads, POST creates, PUT updates and DELETE removes.

What it is

Most Cisco security products expose a REST API, and the exam asks you to read a Python script that calls one.

You do not need to write long programs. You need to follow what each call does: the method, the path, the headers, the body and how the response is used. The Cisco Secure Firewall Management Center (FMC) is a common example.

Scriptrequests libraryPOST credentialsAPItoken endpointreturnsTokenin response headerssent withNext callsGET or POST

How it works

A typical script has three steps: authenticate, call an endpoint, and process the JSON.

A token based example

The script below asks the FMC for a token, then lists network objects. Read it from the top.

import requests
from requests.auth import HTTPBasicAuth

BASE = "FMC_BASE_URL"   # scheme and host of the FMC
USER = "api_user"
PASSWORD = "secret"

# Step 1: authenticate with HTTP basic auth
auth_url = BASE + "/api/fmc_platform/v1/auth/generatetoken"
resp = requests.post(auth_url, auth=HTTPBasicAuth(USER, PASSWORD), verify=False)
resp.raise_for_status()

token = resp.headers["X-auth-access-token"]
domain = resp.headers["DOMAIN_UUID"]

# Step 2: call a configuration endpoint with the token
headers = {"X-auth-access-token": token, "Content-Type": "application/json"}
url = BASE + "/api/fmc_config/v1/domain/" + domain + "/object/networks"
resp = requests.get(url, headers=headers, params={"limit": 25, "offset": 0}, verify=False)

# Step 3: process the JSON
data = resp.json()
for item in data.get("items", []):
    print(item["name"], item["id"])

Reading it line by line

The first call is a POST with basic authentication. A successful reply carries the token and the domain identifier in the response headers, not in the body. Later requests send the token back in the X-auth-access-token header.

The second call is a GET on an object path that includes the domain ID. The params dictionary becomes the query string. The script then turns the body into a Python dictionary with .json() and loops over the items list.

What each element tells you

ElementMeaning
requests.postCreate or submit something, such as a login or a new object
requests.getRead data without changing it
requests.putReplace or update an existing object
requests.deleteRemove an object
headersAuthentication token and content type
paramsQuery string values, often paging or filters
json= or data=The request body that carries the new configuration
verify=FalseSkips certificate validation, which is unsafe outside a lab

Status codes

The response code tells you whether the call worked.

  • 200: success for a read or update.
  • 201: an object was created.
  • 204: success with no body, which some APIs return for a delete or update.
  • 400: the request was malformed.
  • 401: missing or invalid authentication, often an expired token.
  • 403: authenticated but not permitted.
  • 404: the path or object does not exist.
  • 429: too many requests, so slow down.

Pagination and errors

Large result sets are returned in pages. The script uses limit and offset and loops until it has read everything. Good scripts call raise_for_status() or test status_code, and wrap calls in try and except so a failure is clear.

Safe habits

A script should read secrets from environment variables or a vault, not store them in the file. Tokens expire, so a long script must refresh or request a new one. Use certificate validation in production.

What to remember for the exam

Six facts cover most questions on this topic.

  • Authenticate first, then send the token with every later request.
  • On the FMC, the token comes back in the X-auth-access-token response header.
  • GET reads, POST creates, PUT updates and DELETE removes.
  • resp.json() turns the body into a Python dictionary.
  • 401 means authentication failed. 403 means not permitted. 404 means not found.
  • verify=False disables certificate checks and is only for labs.
Exam tip

When a script fails with 401 after working earlier, suspect an expired token before suspecting the path.

Common mistakes

These four mistakes appear often.

Looking for the token in the body. The FMC returns it in the response headers.

Confusing 401 with 403. 401 is an authentication failure, and 403 is a permission failure.

Hardcoding credentials. Secrets in scripts leak through version control.

Ignoring pagination. A script that reads one page misses the rest of the objects.

Watch out

Do not run verify=False against a production system, because it removes protection against a man-in-the-middle attack.

Check yourself

Work through these three questions.

  • Where does the FMC return the access token?

    In the X-auth-access-token response header, which later requests send back as a request header.

  • What does a 403 status code indicate?

    The caller authenticated but is not permitted to perform the action, unlike 401, which means authentication failed.

  • Why does a script use limit and offset?

    To read a large result set page by page, so it does not miss objects beyond the first page.

Topic 7 of 7 in Security Concepts

Test yourself on Security Concepts

The full Cisco CCNP 350-701 SCOR question bank has 822 questions, with timed practice exams and an explanation for every answer.

This study guide is independent and is not affiliated with, endorsed by, or sponsored by Cisco. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.