Skip to content

Commit 75652e3

Browse files
authored
Merge pull request #89 from CiscoDevNet/ag-fix-formatting
Indent Python code block to fix rendering problem
2 parents 5d23546 + 86dc657 commit 75652e3

File tree

1 file changed

+29
-28
lines changed
  • labs/parsing-json

1 file changed

+29
-28
lines changed

labs/parsing-json/1.md

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -39,34 +39,35 @@ To get started, this walk-through shows creating a simple Python script that sen
3939
# We need to import the JSON library just to handle our request to the APIC for login
4040
import json
4141
# We need to log in to the APIC and gather a token, before we can access any data
42-
# Let's construct a request with a body
43-
44-
# We'll need to disable certificate warnings
45-
requests.packages.urllib3.disable_warnings()
46-
47-
# We need to have a body of data consisting of a username and password to gather a cookie from APIC
48-
encoded_body = json.dumps({
49-
"aaaUser": {
50-
"attributes": {
51-
"name": "admin",
52-
"pwd": "ciscopsdt"
42+
# Let's construct a request with a body
43+
44+
# We'll need to disable certificate warnings
45+
requests.packages.urllib3.disable_warnings()
46+
47+
# We need to have a body of data consisting of a username and password to gather a cookie from APIC
48+
encoded_body = json.dumps({
49+
"aaaUser": {
50+
"attributes": {
51+
"name": "admin",
52+
"pwd": "ciscopsdt"
5353
}
5454
}
55-
})
56-
57-
# Now lets make the request and store the data
58-
resp = requests.post("https://sandboxapicdc.cisco.com/api/aaaLogin.json", data=encoded_body, verify=False)
59-
60-
# This stores the received APIC-cookie from the login as a value to be used in subsequent REST calls
61-
header = {"Cookie": "APIC-cookie=" + resp.cookies["APIC-cookie"]}
62-
63-
# Now we make a call towards the tenant class on the ACI fabric with the proper header value set.
64-
# We leverage the .xml ending to receive the data back as XML. We're adding health and faults to the printout to ensure that we get levels of data back from the APIC
65-
tenants = requests.get("https://sandboxapicdc.cisco.com/api/node/class/fvTenant.json?rsp-subtree-include=health,faults", headers=header, verify=False)
66-
67-
# Requests stores the text of the response in the .text attribute. Lets print it to see raw JSON
68-
print(tenants.text)
69-
```
55+
})
56+
57+
# Now lets make the request and store the data
58+
resp = requests.post("https://sandboxapicdc.cisco.com/api/aaaLogin.json", data=encoded_body, verify=False)
59+
60+
# This stores the received APIC-cookie from the login as a value to be used in subsequent REST calls
61+
header = {"Cookie": "APIC-cookie=" + resp.cookies["APIC-cookie"]}
62+
63+
# Now we make a call towards the tenant class on the ACI fabric with the proper header value set.
64+
# We leverage the .xml ending to receive the data back as XML.
65+
# We're adding health and faults to the printout to ensure that we get levels of data back from the APIC
66+
tenants = requests.get("https://sandboxapicdc.cisco.com/api/node/class/fvTenant.json?rsp-subtree-include=health,faults", headers=header, verify=False)
67+
68+
# Requests stores the text of the response in the .text attribute. Lets print it to see raw JSON
69+
print(tenants.text)
70+
```
7071
There is a lot of manipulation going on with the top part of this script, but most of it deals with the methods needed to authenticate towards the ACI fabric, needing a username and password to authenticate, receive the response from the fabric and gather a token, then send this token in a subsequent call to the fabric to pull the ACI tenant information out in JSON format. However, this snippet:
7172

7273
- imported the `request` library to easily interact with the ACI fabric's REST APIs
@@ -76,8 +77,8 @@ There is a lot of manipulation going on with the top part of this script, but mo
7677

7778
4. Save the `get-tenant-json.py` file. To download or review the current code, you can get it from GitHub [here](https://github.com/CiscoDevNet/coding-skills-sample-code/blob/master/coding202-parsing-json/get-tenant-json-1.py).
7879

79-
5. Open a command prompt.<br>
80-
<br>
80+
5. Open a command prompt.
81+
8182
For Windows, enter:
8283

8384
```

0 commit comments

Comments
 (0)