REST Udemy
REST Udemy
• Source: https://restfulapi.net/
CRUD
• Four actions performed by an application:
• Create: Allows the client to create some new instances of
variables and data structures at the server and initialize their
values as kept at the server
• Read: Allows the client to retrieve (read) the current value of
variables that exist at the server, storing a copy of the
variables, structures, and values at the client
• Update: Allows the client to change (update) the value of
variables that exist at the server
• Delete: Allows the client to delete from the server different
instances of data variables
CRUD and HTTP Verbs
REST (HTTP) Verb CRUD Term Action
• URI
• https://sandboxdnac.cisco.com/dna/intent/api/v1/network-
device?type=Cisco ASR 1001-X Router
• Protocol = https
• Server / Host URL = sandboxdnac.cisco.com
• Resource = /dna/intent/api/v1/network-device
• Parameters = ?type=Cisco ASR 1001-X Router
Postman
You need a token
• Authorization is required
• URL: https://sandboxdnac.cisco.com/dna/system/api/v1/auth/token
• Username: devnetuser
• Password: Cisco123!
• Use POST
• Copy the Token you get back
How to get information
• URL: https://sandboxdnac.cisco.com/dna/intent/api/v1/network-device
• Headers
• Content-Type: application/json
• X-Auth-Token: <paste in your token>
• Use GET
Result:
• Get a list of devices
Status Code 200: OK
• Life is good
401 Error Code: Unauthorized
• Token not used
400 Error Code: Bad Request
• network-device123 is not found
REST 404 Error: Not Found
• network-device123 does not exist
Code 404 Error: Not found
• Resource not found
IOS XE
• Use the DevNet IOS XE always on sandbox
• DNS name: ios-xe-mgmt.cisco.com
• Protocol: HTTPS
• Port: 9443
• Username: developer
• Password: C1sco12345
• Application:
• Use Postman
• Free download from https://www.postman.com
Turn of certificate verification
Get interfaces Part 1
• Use the DevNet IOS XE always on sandbox
• URL: https://ios-xe-mgmt.cisco.com:9443/restconf/data/ietf-
interfaces:interfaces
• Method: GET
• Username: developer
• Password: C1sco12345
Get interfaces Part 2
• Add a header
• Key 1:
• Accept
• application/yang-data+json
Get interfaces Part 3
Add an interface Part 1
• Use the DevNet IOS XE always on sandbox
• URL: https://ios-xe-mgmt.cisco.com:9443/restconf/data/ietf-
interfaces:interfaces
• Method: POST
• Username: developer
• Password: C1sco12345
Add Interface Part 2
• Add a header
• Key 1:
• Accept
• application/yang-data+json
• Key 2:
• Content-Type
• Application/yang-data+json
Add Interface Part 3
• Body:
{
"ietf-interfaces:interface": {
"name": "Loopback1234",
"description": "Added with RESTCONF",
"type": "iana-if-type:softwareLoopback",
"enabled": true,
"ietf-ip:ipv4": {
"address": [
{
"ip": "1.2.3.4",
"netmask": "255.255.255.255"
}
]
}
}
}
Add Interface Part 4
• 201 Status means success
Add Interface Part 5
• Use GET to check that interface has been
created
Delete an Interface Part 1
• Use the DevNet IOS XE always on sandbox
• URL: https://ios-xe-mgmt.cisco.com:9443/restconf/data/ietf-
interfaces:interfaces/interface=Loopback1234
• Replace Interface name with correct name.
• Method: DELETE
• Username: developer
• Password: C1sco12345
Delete and Interface Part 2
• Add a header
• Key 1:
• Accept
• application/yang-data+json
Delete and Interface Part 3
• Result:
• Run the get interface command to check
that interface has been removed
Python: Get Interfaces Part 1
import requests
# Credentials
USER = 'developer'
PASS = 'C1sco12345'
Python: Get Interfaces Part 2
# URL for GET request
url = "https://ios-xe-mgmt.cisco.com:9443/restconf/data/ietf-interfaces:interfaces"
# Run GET
response = requests.get(url, auth=(USER, PASS),
headers=headers, verify=False)
# Print results
print('Status Code:' + str(response.status_code))
print('Response Text:' + response.text)
Python: Create Interface Part 1
import requests
# Credentials
USER = 'developer'
PASS = 'C1sco12345'
# Print results
print('Status Code:' + str(response.status_code))
print('Response Text:' + response.text)
Python: Delete Interface Part 1
import requests
# Credentials
USER = 'developer'
PASS = 'C1sco12345'
intname = "Loopback1234"
url = "https://ios-xe-mgmt.cisco.com:9443/restconf/data/ietf-
interfaces:interfaces/interface=" + intname
Python: Delete Interface Part 2
payload = {}
headers = {
'Accept': 'application/yang-data+json',
}
# Credentials
USER = 'developer'
PASS = 'C1sco12345'
url = 'https://ios-xe-mgmt.cisco.com:9443/restconf/data/ietf-interfaces:interfaces'
headers = {
'Accept': 'application/yang-data+json',
'Content-Type': 'application/yang-data+json'
}
Create lots of Interfaces Part 2
int_number = 5
for x in range(int_number):
ipaddr = '1.2.3.' + str(x)
print('Creating loopback :' + ipaddr)
#print (payload)
# Credentials
USER = 'developer'
PASS = 'C1sco12345'
payload = {}
headers = {
'Accept': 'application/yang-data+json',
}
int_number = 5
Delete lots of Interfaces Part 2
for x in range(int_number):
intname = "Loopback123" + str(x)
print('Deleting ' + intname )
url = "https://ios-xe-mgmt.cisco.com:9443/restconf/data/ietf-interfaces:interfaces/interface=" + intname
• https://devnetsandbox.cisco.com/RM/Diagram/In
dex/27d9747a-db48-4565-8d44-
df318fce37ad?diagramType=Topology
REST
Representational State Transfer