0% found this document useful (0 votes)
74 views47 pages

REST Udemy

The document provides information on REST (Representational State Transfer), including its key principles of being client-server, stateless, and cacheable. It also discusses CRUD (Create, Read, Update, Delete) operations and how they map to HTTP verbs. The document then provides examples of using the Cisco DNA Center and IOS XE APIs via REST, including making GET, POST, PUT, PATCH and DELETE requests using Postman and Python code samples.

Uploaded by

AHOUALAKOUN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views47 pages

REST Udemy

The document provides information on REST (Representational State Transfer), including its key principles of being client-server, stateless, and cacheable. It also discusses CRUD (Create, Read, Update, Delete) operations and how they map to HTTP verbs. The document then provides examples of using the Cisco DNA Center and IOS XE APIs via REST, including making GET, POST, PUT, PATCH and DELETE requests using Postman and Python code samples.

Uploaded by

AHOUALAKOUN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

REST

Representational State Transfer


Want to watch a video?
• Part 1: https://youtu.be/5iA_ZBcFwEU
• Part 2: https://youtu.be/p-3QHCt1L_w
• Part 3: https://youtu.be/Bqd901dKIB4
• Part 4: https://youtu.be/kZ4YqqlU5eM
• Part 5: https://youtu.be/DAajWlVcskw
• Part 6: https://youtu.be/umsYXQGV4-g
APIs are everywhere

• We use APIs all the time


REST
• Client–server
• By separating the user interface concerns from the data storage concerns, we
improve the portability of the user interface across multiple platforms and improve
scalability by simplifying the server components.
• Stateless
• Each request from client to server must contain all of the information necessary to
understand the request, and cannot take advantage of any stored context on the
server.
• Session state is therefore kept entirely on the client.
• Cacheable
• Cache constraints require that the data within a response to a request be implicitly
or explicitly labeled as cacheable or non-cacheable. If a response is cacheable,
then a client cache is given the right to reuse that response data for later,
equivalent requests

• 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

POST CREATE (CRUD) Create new data structures


and variables

GET READ (CRUD) Read (Retrieve) variable


names, structures and values

PATCH, PUT UPDATE (CRUD) Update or replace values of


some variable

DELETE DELETE (CRUD) Delete some variables and


data structures
DNA Center
• DevNet DNA Center Always on Lab
• https://sandboxdnac.cisco.com
• User: devnetuser
• Password: Cisco123!
• API documentation:
• https://developer.cisco.com/docs/dna-center/api/1-3-3-
x/?version=1.2.x
• DNA Center hello word documentation
• https://developer.cisco.com/docs/dna-center/#!hello-
world/device-inventory-example
DNA Center Inventory
DNA Center API
REST (Uniform Resource Identifiers) URI

• 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

# Use self signed certs


requests.packages.urllib3.disable_warnings()

# 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"

# Set yang+json as the data formats


headers = {'Content-Type': 'application/yang-data+json',
'Accept': 'application/yang-data+json'}

# 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

# Use self signed certs


requests.packages.urllib3.disable_warnings()

# Credentials
USER = 'developer'
PASS = 'C1sco12345'

# URL for POST request


url = 'https://ios-xe-mgmt.cisco.com:9443/restconf/data/ietf-interfaces:interfaces'
Python: Create Interface Part 2
# JSON Payload - using \ to continue the line in python
payload = '\
{\
"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"\
}\
]\
}\
}\
}'
Python: Create Interface Part 3
# Headers to send
headers = {
'Accept': 'application/yang-data+json',
'Content-Type': 'application/yang-data+json'
}

# Actual post request


response = requests.request('POST',url, auth=(USER, PASS),
headers=headers, data = payload,
verify=False)

# Print results
print('Status Code:' + str(response.status_code))
print('Response Text:' + response.text)
Python: Delete Interface Part 1
import requests

# Use self signed certs


requests.packages.urllib3.disable_warnings()

# 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',
}

response = requests.request("DELETE",url, auth=(USER,


PASS),
headers=headers, data = payload,
verify=False)

print('Status Code:' + str(response.status_code))


print('Response Text:' + response.text)
Create lots of Interfaces Part 1
import requests

# Use self signed certs


requests.packages.urllib3.disable_warnings()

# 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)

# Important – make sure your spacing for the


# subsequent code is right – add 4 spaces to
# left of the code so it is part of the loop.
Create lots of Interfaces Part 3
payload = '\
{\
"ietf-interfaces:interface": {\
"name": "Loopback123' + str(x) + '",\
"description": "Added with RESTCONF",\
"type": "iana-if-type:softwareLoopback",\
"enabled": true,\
"ietf-ip:ipv4": {\
"address": [\
{\
"ip": "1.2.3.' + str(x) + '",\
"netmask": "255.255.255.255"\
}\
]\
}\
}\
}'
Create lots of Interfaces Part 4

#print (payload)

response = requests.request('POST',url, auth=(USER, PASS),


headers=headers, data = payload, verify=False)

print('Status Code:' + str(response.status_code))


print('Response Text:' + response.text)
Delete lots of Interfaces Part 1
import requests

# Use self signed certs


requests.packages.urllib3.disable_warnings()

# 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

response = requests.request("DELETE",url, auth=(USER, PASS),


headers=headers, data = payload, verify=False)

print('Status Code:' + str(response.status_code))


print('Response Text:' + response.text)
Good DevNet labs
• Go here for more:
• https://developer.cisco.com/learning/tracks/iosxe-
programmability/intro-device-level-
interfaces/intro-restconf/step/1

• https://devnetsandbox.cisco.com/RM/Diagram/In
dex/27d9747a-db48-4565-8d44-
df318fce37ad?diagramType=Topology
REST
Representational State Transfer

You might also like