In the previous lesson, you learned to read Python data structures by hand.
Now you need a library to fetch them from a router. The most common one isrequests.It handles opening the HTTPS connection, sending the request, and returning the response:
import requests response = requests.get("https://192.168.1.1/restconf/data/...")The data inside that response arrives in JSON.
Two specific details in this section trip up candidates on the exam.JSON vs Python Dict
When Python talks to a Cisco device, both sides exchange data as JSON text.
JSON looks almost identical to a Python dictionary, with three key differences:{ "hostname": "R1", "port": 830, "enabled": true, "interfaces": ["Gi0/0", "Gi0/1"] }Rule
JSON
Python
String quotes
Always double quotes
Single or double quotes
Booleans
true / false
True / False
Null value
null
None
Table 1 – JSON vs Python syntax
If an exam snippet uses single quotes or capitalized
Trueinside JSON, it is invalid syntax.Answer the question below
Is { "name": "R1", "alive": True } valid JSON? (yes or no)
Response Parsing
When your script receives JSON from a device, the data arrives as a plain text string. You cannot index a string by key, so you convert it into a Python dict first:
import requests response = requests.get(url) print(type(response.text)) print(type(response.json()))Output:
<class 'str'> <class 'dict'>response.text: Returns raw JSON string. Indexing with key names likeresponse.text["hostname"]crashes.response.json(): Parses the JSON string and returns a Python dictionary you can index normally.
You have
You get
You can do
response.textstring
String operations only
response.json()dict
Index with
["key"], loop, slicejson.loads(text)dict
Same as response.json()
json.dumps(d)string
Convert a Python dict back to a JSON string (for sending)
Table 2 – Working with JSON in your script
Validate this Part 1 layout, and we will move straight to Part 2 (The HTTP Rules).
Answer the question below
Which method on a response object returns a Python dict?
Answer the question below
After data = response.json() on that JSON example, which class name does type(data["interfaces"]) show?
Four elements shape every RESTCONF call you will read on the exam: the URL structure, the HTTP method, the response status code, and the request headers.

Figure 1 – The four things that shape every RESTCONF call
RESTCONF URL Structure
Every RESTCONF URL follows a predictable pattern. Read this example URL:
https://192.168.1.1/restconf/data/Cisco-IOS-XE-native:native/interface/GigabitEthernet=0%2F1Break it down into three logical blocks:
[https://192.168.1.1/restconf/data/](https://192.168.1.1/restconf/data/): The fixed RESTCONF entry point on R1.Cisco-IOS-XE-native:native: The YANG module and root container.interface/GigabitEthernet=0%2F1: The path to the specific interface.
The characters
%2Frepresent the URL-encoded slash/.
Therefore,GigabitEthernet=0%2F1points directly to interfaceGigabitEthernet0/1.Answer the question below
In a RESTCONF URL, what single character does %2F represent?
HTTP Methods
RESTCONF maps device operations to five standard HTTP methods.
Each method corresponds to a specific function in therequestslibrary.Method
Action on R1
Python call
GET
Read configuration or operational data
requests.get(...)POST
Create a new resource (must not exist)
requests.post(...)PUT
Replace the entire resource
requests.put(...)PATCH
Modify part of the resource
requests.patch(...)DELETE
Remove the resource
requests.delete(...)Table 3 – HTTP methods used by RESTCONF
On the exam, distinguishing
PUTfromPATCHis critical.PUTreplaces the complete resource, whereasPATCHonly updates the specified fields.Answer the question below
Which requests function sends an HTTP GET to a router?
HTTP Status Codes
Every response from R1 includes a numerical status code accessible via
response.status_code.Code
Name
Meaning
200
OK
GET worked, body contains the data
201
Created
POST created a new resource
204
No Content
PATCH or PUT applied, no body returned
400
Bad Request
Your payload or URL is malformed
401
Unauthorized
Wrong username or password in
auth=()404
Not Found
The URL points to a resource that does not exist
Table 4 – HTTP status codes you will see in exam exhibits
Codes starting with 2 indicate success.
Codes starting with 4 signal a client-side error in authentication, payload, or URL formatting.Answer the question below
What does a status code that starts with 4 mean?
RESTCONF Headers
RESTCONF scripts carry two standard HTTP headers to define data formats:
Accept: Tells R1 the expected response format. Set toapplication/yang-data+jsonfor JSON.Content-Type: Tells R1 the format of the sent payload. Required forPOST,PUT, andPATCH.
GETrequests only require theAcceptheader because they carry no payload body.POSTandPATCHrequests require both headers.Answer the question below
Which HTTP method would you use to create a new resource that does not exist yet?
Answer the question below
Which header tells R1 the format of the body you are sending in a PATCH request?