28.1.5 Lab - Use RESTCONF To Access An IOS XE Device
28.1.5 Lab - Use RESTCONF To Access An IOS XE Device
Topology
Addressing Table
Device Interface IP Address Subnet Mask
Objectives
Part 1: Build the Network and Verify Connectivity
Part 2: Configure an IOS XE Device for RESTCONF Access
Part 3: Open and Configure Postman
Part 4: Use Postman to Send GET Requests
Part 5: Use Postman to Send a PUT Request
Part 6: Use a Python Script to Send GET Requests
Part 7: Use a Python Script to Send a PUT Request
Background / Scenario
The RESTCONF protocol is a subset of NETCONF. RESTCONF allows you to make RESTful API calls to an
IOS XE device. The data that is returned by the API can be either XML or JSON. In the first half of this lab,
you will use the Postman program to construct and send API requests to the RESTCONF service that is
running on R1. In the second half of the lab, you will create Python scripts to perform the same tasks as your
Postman program.
Required Resources
1 Router (Cisco 4221 with Cisco IOS XE Release 16.9.4 universal image or comparable)
1 Switch (Optional: any switch available for connecting R1 and the PC)
1 PC (Choice of operating system with Cisco Networking Academy CCNP VM running in a virtual
machine client and terminal emulation program)
Ethernet cables as shown in the topology
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 1 of 16 www.netacad.com
Lab - Use RESTCONF to Access an IOS XE Router
Instructions
enable
configure terminal
hostname R1
no ip domain lookup
line con 0
logging synchronous
exec-timeout 0 0
line vty 0 15
exec-t 0 0
logg sync
login local
transport input ssh
!Configure SSH which is required for NETCONF and RESTCONF access
ip domain name example.netacad.com
crypto key generate rsa modulus 2048
username cisco privilege 15 password cisco123!
interface GigabitEthernet0/0/1
description Link to PC
ip address 192.168.1.1 255.255.255.0
no shutdown
!Configure a DHCP server to assign IPv4 addressing to the CCNP VM
ip dhcp excluded-address 192.168.1.1 192.168.1.10
ip dhcp pool LAN
network 192.168.1.0 /24
default-router 192.168.1.1
domain-name example.netacad.com
end
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 2 of 16 www.netacad.com
Lab - Use RESTCONF to Access an IOS XE Router
c. If the CCNP VM has not received IPv4 addressing, check your physical connections between the host PC
and R1. Also, verify that R1 is configured correctly according to the previous step.
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 3 of 16 www.netacad.com
Lab - Use RESTCONF to Access an IOS XE Router
Note: The purpose and function of all the daemons is beyond the scope of this course.
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 4 of 16 www.netacad.com
Lab - Use RESTCONF to Access an IOS XE Router
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 5 of 16 www.netacad.com
Lab - Use RESTCONF to Access an IOS XE Router
b. Under Type, click the down arrow next to “Inherit auth from parent” and choose Basic Auth.
c. For Username and Password, enter the local authentication credentials that were configured on R1 in
Part 1:
Username: cisco
Password: cisco123!
Step 5: Set JSON as the data type to send to and receive from R1.
You can send and receive data from R1 in XML or JSON format. For this lab, you will use JSON.
a. Above the Temporary Headers area is the Headers area. Click in the Key field and type Content-Type
for the type of key. In the Value field, type application/yang-data+json. This tells Postman to send
JSON data to R1.
b. Below your Content-Type key, add another key/value pair. The Key field is Accept and the Value field is
application/yang-data+json.
Note: You can change application/yang-data+json to application/yang-data+xml to send and receive XML
data instead of JSON data, if necessary.
This JSON response verifies that Postman can now send other REST API requests to R1.
Step 7: Use a GET request to gather the information for all interfaces on R1.
a. Now that you have a successful GET request, you can use it as a template for additional requests. At the
top of Postman, next to the Launchpad tab, right-click the GET tab that you just used and choose
Duplicate Current Tab.
b. Use the ietf-interfaces YANG model to gather interface information. For the URL, add data/ietf-
interfaces:interfaces:
https://192.168.1.1/restconf/data/ietf-interfaces:interfaces
c. Click Send. You should see a JSON response from R1 that is similar to the output shown below. Your
output may be different depending on your particular router.
{
"ietf-interfaces:interfaces": {
"interface": [
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 6 of 16 www.netacad.com
Lab - Use RESTCONF to Access an IOS XE Router
{
"name": "GigabitEthernet0",
"type": "iana-if-type:ethernetCsmacd",
"enabled": false,
"ietf-ip:ipv4": {},
"ietf-ip:ipv6": {}
},
{
"name": "GigabitEthernet0/0/0",
"type": "iana-if-type:ethernetCsmacd",
"enabled": false,
"ietf-ip:ipv4": {},
"ietf-ip:ipv6": {}
},
{
"name": "GigabitEthernet0/0/1",
"description": "Link to PC",
"type": "iana-if-type:ethernetCsmacd",
"enabled": true,
"ietf-ip:ipv4": {
"address": [
{
"ip": "192.168.1.1",
"netmask": "255.255.255.0"
}
]
},
"ietf-ip:ipv6": {}
}
]
}
}
Step 8: Use a GET request to gather information for a specific interface on R1.
In this lab, only the GigabitEthernet 0/0/0 interface is configured. To specify just this interface, extend the URL
to only request information for this interface.
a. Duplicate your last GET request.
b. Add the interface= parameter to specify an interface and type in the name of the interface. However,
notice that you need to use the HTML code %2F for the forward slashes in the interface name. So, 0/0/1
becomes 0%2F0%2F1.
https://192.168.1.1/restconf/data/ietf-interfaces:interfaces/interface=GigabitEthernet0%2F0%2F1
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 7 of 16 www.netacad.com
Lab - Use RESTCONF to Access an IOS XE Router
"type": "iana-if-type:ethernetCsmacd",
"enabled": true,
"ietf-ip:ipv4": {
"address": [
{
"ip": "192.168.1.1",
"netmask": "255.255.255.0"
}
]
},
"ietf-ip:ipv6": {}
}
}
Step 2: Configure the body of the request specifying the information for the new loopback.
a. To send a PUT request, you need to provide the information for the body of the request. Next to the
Headers tab, click Body. Then click the Raw radio button. The field is currently empty. If you click Send
now, you will get error code 400 Bad Request because Loopback1 does not exist yet and you did not
provide enough information to create the interface.
b. Replace the text in brackets in the description field with your name. Remove the brackets. This will add
your name to the interface description.
c. Fill in the Body section with the required JSON data to create a new Loopback1 interface. You can copy
the Body section of the previous GET request and modify it. Or you can copy the following into the Body
section of your PUT request. Notice that the type of interface must be set to softwareLoopback.
{
"ietf-interfaces:interface": {
"name": "Loopback1",
"description": "[Student Name]'s Loopback",
"type": "iana-if-type:softwareLoopback",
"enabled": true,
"ietf-ip:ipv4": {
"address": [
{
"ip": "10.1.1.1",
"netmask": "255.255.255.0"
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 8 of 16 www.netacad.com
Lab - Use RESTCONF to Access an IOS XE Router
}
]
},
"ietf-ip:ipv6": {}
}
}
d. Click Send to send the PUT request to R1. In the response field, you should get a 201 Created HTTP
response code. This indicates that the resource was created successfully.
e. You can verify that the interface was created by entering show ip interface brief on R1. You can also
run the Postman tab that contains the request to get information about the interfaces on R1 that was
created in Part 4 of this lab.
Open configuration window
Step 2: Create the variables that will be the components of the request.
Create a string variable to hold the API endpoint URI and two dictionaries, one for the request headers and
one for the body JSON. Notice that these are all the same tasks you completed in the Postman application.
a. Create a variable named api_url and assign it the URL that will access the interface information on R1.
api_url = "https://192.168.1.1/restconf/data/ietf-interfaces:interfaces"
b. Create a dictionary variable named headers that has keys for Accept and Content-type and assign the
keys the value application/yang-data+json.
headers = { "Accept": "application/yang-data+json",
"Content-type":"application/yang-data+json"
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 9 of 16 www.netacad.com
Lab - Use RESTCONF to Access an IOS XE Router
c. Create a Python tuple variable named basicauth that has two keys needed for authentication, username
and password.
basicauth = ("cisco", "cisco123!")
Step 3: Create a variable to send the request and store the JSON response.
Use the variables that were created in the previous step as parameters for the requests.get() method. This
method sends an HTTP GET request to the RESTCONF API on R1. Assign the result of the request to a
variable named resp. That variable will hold the JSON response from the API. If the request is successful, the
JSON will contain the returned YANG data model.
a. Enter the following statement:
resp = requests.get(api_url, auth=basicauth, headers=headers, verify=False)
Element Explanation
c. Save and run your script. You should get the output shown below. If not, verify all previous steps in this
part as well as the SSH and RESTCONF configuration for R1.
================= RESTART: /home/student/restconf-script.py =================
<Response [200]>
>>>
Step 4: Format and display the JSON data received from R1.
Now the YANG model response values can be extracted from the response JSON.
a. The response JSON is not compatible with Python dictionary and list objects, so it must be converted to
Python format. Create a new variable called response_json and assign the variable resp to it. Add the
json() method to convert the JSON. The statement is as follows:
response_json = resp.json()
b. Add a print statement to display the JSON data.
print(response_json)
c. Save and run your script. You should get output similar to the following:
================= RESTART: /home/student/restconf-script.py =================
<Response [200]>
{'ietf-interfaces:interfaces': {'interface': [{'name': 'GigabitEthernet0', 'type':
'iana-if-type:ethernetCsmacd', 'enabled': False, 'ietf-ip:ipv4': {}, 'ietf-ip:ipv6':
{}}, {'name': 'GigabitEthernet0/0/0', 'type': 'iana-if-type:ethernetCsmacd',
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 10 of 16 www.netacad.com
Lab - Use RESTCONF to Access an IOS XE Router
d. To prettify the output, edit your print statement to use the json.dumps() function with the “indent”
parameter:
print(json.dumps(response_json, indent=4))
e. Save and run your script. You should get the output shown below. This output is virtually identical to the
output of your first Postman GET request.
Note: The following output is truncated to display only interfaces with a configuration.
================= RESTART: /home/student/restconf-script.py =================
<Response [200]>
{
"ietf-interfaces:interfaces": {
"interface": [
(output omitted)
"name": "GigabitEthernet0/0/1",
"description": "Link to PC",
"type": "iana-if-type:ethernetCsmacd",
"enabled": true,
"ietf-ip:ipv4": {
"address": [
{
"ip": "192.168.1.1",
"netmask": "255.255.255.0"
}
]
},
"ietf-ip:ipv6": {}
},
{
"name": "Loopback1",
"description": "[Student's Name] Loopback",
"type": "iana-if-type:softwareLoopback",
"enabled": true,
"ietf-ip:ipv4": {
"address": [
{
"ip": "10.1.1.1",
"netmask": "255.255.255.0"
}
]
},
"ietf-ip:ipv6": {}
}
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 11 of 16 www.netacad.com
Lab - Use RESTCONF to Access an IOS XE Router
]
}
}
Step 2: Create the variables that will be the components of the request.
Create a string variable to hold the API endpoint URI and two dictionaries, one for the request headers and
one for the body JSON. Notice that these are all the same tasks you completed in the Postman application.
a. Create a variable named api_url and assign it the URL that targets a new Loopback2 interface.
Note: This variable specification should be on one line in your script.
api_url = "https://192.168.1.1/restconf/data/ietf-interfaces:interfaces/interface=Loopback2"
b. Create a dictionary variable named headers that has keys for Accept and Content-type and assign the
keys the value application/yang-data+json.
headers = { "Accept": "application/yang-data+json",
"Content-type":"application/yang-data+json"
}
c. Create a Python tuple variable named basicauth that has two values needed for authentication,
username and password.
basicauth = ("cisco", "cisco123!")
d. Create a Python dictionary variable yangConfig that will hold the YANG data that is required to create
new interface Loopback2. You can use the same dictionary that you used in Part 5 for Postman.
However, change the interface number and address. Also, be aware that Boolean values must be
capitalized in Python. Therefore, make sure that the T is capitalized in the key/value pair for “enabled”:
True.
yangConfig = {
"ietf-interfaces:interface": {
"name": "Loopback2",
"description": "[Student\’s Name] loopback interface",
"type": "iana-if-type:softwareLoopback",
"enabled": True,
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 12 of 16 www.netacad.com
Lab - Use RESTCONF to Access an IOS XE Router
"ietf-ip:ipv4": {
"address": [
{
"ip": "10.2.1.1",
"netmask": "255.255.255.0"
}
]
},
"ietf-ip:ipv6": {}
}
}
Note: If using an apostrophe in the description text string, then you must escape it with the backslash, as
shown above.
Step 3: Create a variable to send the request and store the JSON response.
Use the variables created in the previous step as parameters for the requests.put() method. This method
sends an HTTP PUT request to the RESTCONF API. Assign the result of the request to a variable named
resp. That variable will hold the JSON response from the API. If the request is successful, the JSON will
contain the returned YANG data model.
a. Before entering statements, please note that this variable specification should be on only one line in your
script. Enter the following statements:
Note: This variable specification should be on one line in your script.
resp = requests.put(api_url, data=json.dumps(yangConfig), auth=basicauth,
headers=headers, verify=False)
b. Enter the code below to handle the response. If the the response is one of the HTTP success messages,
the first message will be printed. Any other code value is considered an error. The response code and
error message will be printed in the event that an error has been detected.
if(resp.status_code >= 200 and resp.status_code <= 299):
print("STATUS OK: {}".format(resp.status_code))
else:
print('Error. Status Code: {} \nError message: {}'.format(resp.status_code,resp.json()))
Element Explanation
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 13 of 16 www.netacad.com
Lab - Use RESTCONF to Access an IOS XE Router
c. Run the script to send the PUT request to R1. You should get a 201 Status Created message. If not,
check your code and the configuration for R1.
d. You can verify that the interface was created by entering show ip interface brief on R1.
Open configuration window
api_url = "https://192.168.1.1/restconf/data/ietf-interfaces:interfaces"
print(resp)
response_json = resp.json()
print(json.dumps(response_json, indent=4))
api_url =
"https://192.168.1.1/restconf/data/ietf-interfaces:interfaces/interface=Loopback2"
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 14 of 16 www.netacad.com
Lab - Use RESTCONF to Access an IOS XE Router
"Content-type":"application/yang-data+json"
}
basicauth = ("cisco", "cisco123!")
yangConfig = {
"ietf-interfaces:interface": {
"name": "Loopback2",
"description": "[Student\'s Name] loopback interface",
"type": "iana-if-type:softwareLoopback",
"enabled": True,
"ietf-ip:ipv4": {
"address": [
{
"ip": "10.2.1.1",
"netmask": "255.255.255.0"
}
]
},
"ietf-ip:ipv6": {}
}
}
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 15 of 16 www.netacad.com
Lab - Use RESTCONF to Access an IOS XE Router
Router Model Ethernet Interface #1 Ethernet Interface #2 Serial Interface #1 Serial Interface #2
Note: To find out how the router is configured, look at the interfaces to identify the type of router and how many
interfaces the router has. There is no way to effectively list all the combinations of configurations for each router
class. This table includes identifiers for the possible combinations of Ethernet and Serial interfaces in the device.
The table does not include any other type of interface, even though a specific router may contain one. An example
of this might be an ISDN BRI interface. The string in parenthesis is the legal abbreviation that can be used in
Cisco IOS commands to represent the interface.
End of document
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 16 of 16 www.netacad.com