8.3.7 Lab - Use RESTCONF To Access An IOS XE Device - ILM
8.3.7 Lab - Use RESTCONF To Access An IOS XE Device - ILM
Version)
Instructor Note: Red font color or gray highlights indicate text that appears in the instructor copy only.
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 provides a simplified subset of NETCONF features over a RESTful API.
RESTCONF allows you to make RESTful API calls to an IOS XE device. The data that is returned by the API
can be formatted in 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 the CSR1kv. In the second half
of the lab, you will create Python scripts to perform the same tasks as your Postman program.
Required Resources
1 PC with operating system of your choice
Virtual Box or VMWare
DEVASC Virtual Machine
CSR1kv Virtual Machine
Instructions
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 1 of 14 www.netacad.com
Lab - Use RESTCONF to Access an IOS XE Device
c. Ping the CSR1kv to verify connectivity. You should have already done this in the installation labs. If you
are not able to ping, then revisit those labs listed above in Part 1a.
devasc@labvm:~$ ping -c 5 192.168.56.101
PING 192.168.56.101 (192.168.56.101) 56(84) bytes of data.
64 bytes from 192.168.56.101: icmp_seq=1 ttl=254 time=1.37 ms
64 bytes from 192.168.56.101: icmp_seq=2 ttl=254 time=1.15 ms
64 bytes from 192.168.56.101: icmp_seq=3 ttl=254 time=0.981 ms
64 bytes from 192.168.56.101: icmp_seq=4 ttl=254 time=1.01 ms
64 bytes from 192.168.56.101: icmp_seq=5 ttl=254 time=1.14 ms
Note: The first time you SSH to the CSR1kv, your DEVASC VM warns you about the authenticity of the
CSR1kv. Because you trust the CSR1kv, answer yes to the prompt.
The authenticity of host '192.168.56.101 (192.168.56.101)' can't be
established.
RSA key fingerprint is SHA256:HYv9K5Biw7PFiXeoCDO/LTqs3EfZKBuJdiPo34VXDUY.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.56.101' (RSA) to the list of known hosts.
b. Enter cisco123! as the password and you should now be at the privileged EXEC command prompt for
the CSR1kv.
Password: <cisco123!>
CSR1kv#
c. Leave the SSH session open for the next Part.
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 2 of 14 www.netacad.com
Lab - Use RESTCONF to Access an IOS XE Device
nesd : Running
syncfd : Running
ncsshd : Running
dmiauthd : Running
nginx : Running
ndbmand : Running
pubd : Running
CSR1kv#
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 3 of 14 www.netacad.com
Lab - Use RESTCONF to Access an IOS XE Device
pubd : Running
Close configuration window
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 4 of 14 www.netacad.com
Lab - Use RESTCONF to Access an IOS XE Device
Step 4: Set JSON as the data type to send to and receive from the CSR1kv.
You can send and receive data from the CSR1kv in XML or JSON format. For this lab, you will use JSON.
a. In the Headers area, click in the first blank 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 the CSR1kv.
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 the CSR1kv.
Step 6: Use a GET request to gather the information for all interfaces on the CSR1kv.
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 Tab.
b. Use the ietf-interfaces YANG model to gather interface information. For the URL, add data/ietf-
interfaces:interfaces:
https://192.168.56.101/restconf/data/ietf-interfaces:interfaces
c. Click Send. You should see a JSON response from the CSR1kv that is similar to the output shown below.
Your output may be different depending on your particular router.
{
"ietf-interfaces:interfaces": {
"interface": [
{
"name": "GigabitEthernet1",
"description": "VBox",
"type": "iana-if-type:ethernetCsmacd",
"enabled": true,
"ietf-ip:ipv4": {},
"ietf-ip:ipv6": {}
}
]
}
}
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 5 of 14 www.netacad.com
Lab - Use RESTCONF to Access an IOS XE Device
Step 7: Use a GET request to gather information for a specific interface on the CSR1kv.
In this lab, only the GigabitEthernet1 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.
https://192.168.56.101/restconf/data/ietf-interfaces:interfaces/interface=GigabitEthernet1
Note: If you request interface information from a different Cisco device with names that use forward
slashes, such as GigabitEthernet0/0/1, use the HTML code %2F for the forward slashes in the interface
name. So, 0/0/1 becomes 0%2F0%2F1.
c. Click Send. You should see a JSON response from the CSR1kv that is similar to output below. Your
output may be different depending on your particular router. In the default CSR1kv setup, you will not see
IP addressing information.
{
"ietf-interfaces:interface": {
"name": "GigabitEthernet1",
"description": "VBox",
"type": "iana-if-type:ethernetCsmacd",
"enabled": true,
"ietf-ip:ipv4": {},
"ietf-ip:ipv6": {}
}
}
d. This interface receives addressing from a Virtual Box template. Therefore, the IPv4 address is not shown
under show running-config. Instead, you will see the ip address dhcp command. You can see this also
in the show ip interface brief output.
CSR1kv# show ip interface brief
Interface IP-Address OK? Method Status Protocol
GigabitEthernet1 192.168.56.101 YES DHCP up up
CSR1kv#
e. In the next Part you will need to use the JSON response from a manually configurated interface. Open a
command terminal with the CSR1kv and manually configure the GigabitEthernet1 interface with the same
IPv4 address currently assigned to it by Virtual Box.
CSR1kv# conf t
CSR1kv(config)# interface g1
CSR1kv(config-if)# ip address 192.168.56.101 255.255.255.0
CSR1kv(config-if)# end
CSR1kv# show ip interface brief
Interface IP-Address OK? Method Status Protocol
GigabitEthernet1 192.168.56.101 YES manual up up
CSR1kv#
f. Return to Postman and send your GET request again. You should now see IPv4 addressing information
in the JSON response, as shown below. In the next Part, you will copy this JSON format to create a new
interface.
{
"ietf-interfaces:interface": {
"name": "GigabitEthernet1",
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 6 of 14 www.netacad.com
Lab - Use RESTCONF to Access an IOS XE Device
"description": "VBox",
"type": "iana-if-type:ethernetCsmacd",
"enabled": true,
"ietf-ip:ipv4": {
"address": [
{
"ip": "192.168.56.101",
"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. 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": "My first RESTCONF 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 7 of 14 www.netacad.com
Lab - Use RESTCONF to Access an IOS XE Device
]
},
"ietf-ip:ipv6": {}
}
}
c. Click Send to send the PUT request to the CSR1kv. Below the Body section, you should see the HTTP
response code Status: 201 Created. This indicates that the resource was created successfully.
d. You can verify that the interface was created. Return to your SSH session with the CSR1kv and enter
show ip interface brief. You can also run the Postman tab that contains the request to get information
about the interfaces on the CSR1kv that was created in the previous Part of this lab.
Open configuration window
Step 2: Create the variables that will be the components of the request.
a. Create a variable named api_url and assign it the URL that will access the interface information on the
CSR1kv.
api_url = "https://192.168.56.101/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 8 of 14 www.netacad.com
Lab - Use RESTCONF to Access an IOS XE Device
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 the CSR1kv. 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 the CSR1kv.
devasc@labvm:~/labs/devnet-src$ cd restconf/
devasc@labvm:~/labs/devnet-src/restconf$ python3 restconf-get.py
<Response [200]>
devasc@labvm:~/labs/devnet-src/restconf$
Step 4: Format and display the JSON data received from the CSR1kv.
Now you can extract the YANG model response values 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:
devasc@labvm:~/labs/devnet-src/restconf$ python3 restconf-get.py
<Response [200]>
{'ietf-interfaces:interfaces': {'interface': [{'name': 'GigabitEthernet1',
'description': 'VBox', 'type': 'iana-if-type:ethernetCsmacd', 'enabled': True, 'ietf-
ip:ipv4': {'address': [{'ip': '192.168.56.101', 'netmask': '255.255.255.0'}]}, 'ietf-
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 9 of 14 www.netacad.com
Lab - Use RESTCONF to Access an IOS XE Device
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.
devasc@labvm:~/labs/devnet-src/restconf$ python3 restconf-get.py
<Response [200]>
{
"ietf-interfaces:interfaces": {
"interface": [
{
"name": "GigabitEthernet1",
"description": "VBox",
"type": "iana-if-type:ethernetCsmacd",
"enabled": true,
"ietf-ip:ipv4": {
"address": [
{
"ip": "192.168.56.101",
"netmask": "255.255.255.0"
}
]
},
"ietf-ip:ipv6": {}
},
{
"name": "Loopback1",
"description": "My first RESTCONF 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": {}
}
]
}
}
devasc@labvm:~/labs/devnet-src/restconf$
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 10 of 14 www.netacad.com
Lab - Use RESTCONF to Access an IOS XE Device
Step 2: Create the variables that will be the components of the request.
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.56.101/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
the new interface Loopback2. You can use the same dictionary that you used previously in 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": "My second RESTCONF loopback",
"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 11 of 14 www.netacad.com
Lab - Use RESTCONF to Access an IOS XE Device
}
}
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 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
c. Save and run the script to send the PUT request to the CSR1kv. You should get a 201 Status Created
message. If not, check your code and the configuration for the CSR1kv.
d. You can verify that the interface was created by entering show ip interface brief on the CSR1kv.
Open configuration window
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 12 of 14 www.netacad.com
Lab - Use RESTCONF to Access an IOS XE Device
api_url = "https://192.168.56.101/restconf/data/ietf-interfaces:interfaces"
print(resp)
response_json = resp.json()
print(json.dumps(response_json, indent=4))
#end of file
#===================================================================
#resconf-put.py
import json
import requests
requests.packages.urllib3.disable_warnings()
api_url =
"https://192.168.56.101/restconf/data/ietf-interfaces:interfaces/interface=Loopback2"
yangConfig = {
"ietf-interfaces:interface": {
"name": "Loopback2",
"description": "My second RESTCONF loopback",
"type": "iana-if-type:softwareLoopback",
"enabled": True,
"ietf-ip:ipv4": {
"address": [
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 13 of 14 www.netacad.com
Lab - Use RESTCONF to Access an IOS XE Device
{
"ip": "10.2.1.1",
"netmask": "255.255.255.0"
}
]
},
"ietf-ip:ipv6": {}
}
}
#end of file
End of document
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 14 of 14 www.netacad.com