SlideShare a Scribd company logo
Coding Skills 101: How to
call REST APIs from
Python
Amanda Whaley – DevNet Community Manager
amwhaley@cisco.com
@mandywhaley
Agenda
• Who is this session for?
• REST Web Service Basics
• Try the APIC-EM APIs from
Postman
• Python Environment Setup
• Python Examples
• NEXT UI Toolkit
Who is Coding 101 for?
New coders
Returning coders
NetOps
DevOps
Congratulations! You have learned:
4
• REST Web Service Basics
• Anatomy of a REST API Request
• GET and POST
• How to call the APIC-EM APIs from Postman
• How to call REST APIs from Python
• Requests Library
Disclaimer: Sample Code vs. Real Code
5
The examples and code in this presentation are for Learning and
Educational purposes.
The samples were created with the goals of clarity and ease of
understanding.
If you are writing code for a real application, you would write the code
in a more efficient and structured style.
Follow along with the Learning Lab
6
• Login with DevNet/Cisco.com ID
• Coding 101 Lab
• Coding 102 Lab
• Taking it further: Coding 201-207 + APIC-EM Learning Labs
Follow Coding 102 instructions to setup your system
• Chrome
• A text editor (text wrangler, notepad ++, sublime text etc.)
• Postman REST Client -- http://www.getpostman.com/
• Python
• Go to command prompt and type python3 – did it work?
• http://learnpythonthehardway.org/book/ex0.html
• Python Requests library -- http://docs.python-requests.org
https://learninglabs.cisco.com
DevNet APIC-EM Always On Sandbox
7
Always there for you to use…
• Register on Cisco DevNet (http://developer.cisco.com ) to get the latest
information about APIs as they are release
• You can use the Always-On APIC-EM Sandbox at anytime
• You can use https://learninglabs.cisco.com at anytime
https://sandboxapic.cisco.com/
DevNet GitHub
8
coding-skills-sample-code repository
• All the code for this session
• All the code for all of the Coding Skills Learning Labs
https://github.com/CiscoDevNet
So what is a REST Web Service?
9
What is a Web Service?
– A way for two systems to communicate through a defined interface.
– Two major types of Web Services – REST or SOAP
What is a REST Web Service?
– REST is an architecture style for designing networked applications.
– A REST web service is a web service that is as easy to call as making an
HTTP request.
– RESTful interfaces often offer the CRUD operations
What is so great about REST?
10
• Hosts
• Devices
• Policies
• Applications
Easy to use:
• In mobile apps
• In console apps
• In web apps
How does this work?
Cisco APIC-EM REST APIs
How does this work?
8
3rd Party
App
3rd Party
App
Request
Response
Get Hosts
List of Hosts
Cisco APIC-EM
Cisco APIC-EM
APIC-EM Example: Get Hosts
12
3rd Party
App
GET http://{APIC-EMController}/api/v0/host/{start}/{no. rec}
List of Hosts returned in JSON
Cisco APIC-EM
APIC-EM Learning labs
Application Policy Infrastructure Controller (APIC) Enterprise Module (EM)
Request
Response
Anatomy of a REST Request
13
Method
– GET, POST, PUT, DELETE
URL
– Example: http://{APIC-EMController}/api/v0/host/1/3
Authentication
– Basic HTTP, OAuth, none
Custom Headers
– HTTP Headers
– Example: Content-Type: application/json
Request Body
– JSON or XML containing data needed to complete request
JSON -- JavaScript Object Notation, is a lightweight text-based open standard designed for human-readable data
interchange.
Using the API Reference Documentation
Using the API Reference Documentation
And what is in the Response?
16
HTTP Status Codes
– http://www.w3.org/Protocols/HTTP/HTRESP.html
– 200 OK
– 201 Created
– 500 Internal Error
Headers
Body
– JSON
– XML
REST in Action: How can I try it?
17
HTTP clients can help you quickly test web services
 Postman - http://www.getpostman.com/
 Firefox RestClient - https://addons.mozilla.org/en-US/firefox/addon/restclient/
 Command Line using curl - http://curl.haxx.se/docs/httpscripting.html#GET
 SOAPUI
Many IDEs have consoles for testing REST Services built in
We are going to use Postman as an example.
REST Demo – Using Postman
18
REST Demo – Using Postman
19
REST Demo – Using Postman
20
 Get Hosts
– Method: GET
– URL: http://APIC-EMController/api/v0/host/{startIndex}/{recordsToReturn}
 Get Devices
– Method: GET
– URL: http://APIC-EMController/api/v0/network-device/{startIndex}/{recordsToReturn}
 Get Policies
– Method: GET
– URL: http://APIC-EMController/api/v0/policy/{startIndex}/{recordsToReturn}
 Get Applications
– Method: GET
– URL: http://APIC-EMController/api/v0/application/{startIndex}/{recordsToReturn}
REST DEMO – Using the POST or PUT Method
21
To send data to a REST service and either create or update data, you will need
to use POST or PUT.
Create Policy Example
– Method: POST
– URL: http://APIC-EMController/api/v0/policy
– Custom Headers: Content-Type: application/json
– Request Body: JSON that specifies details of new policy
What if the Content-Type header is missing?
What if there is a mistake in the JSON Request Body?
Handy tool for validating JSON -- http://jsonlint.com/
REST Demo – Using POST or PUT Method
22
If you didn't receive a 202 Accepted message, check the following:
 Is your URI correct?
 Did you choose the method to be Post?
 Did you remember to specify the Content-Type header as
application/json?
 Did you get a message indicating that the policy already exists?
 If so, try it again with a different port (please use only high ports) or host.
Wait that didn’t work!??!
 Verify that your new policy exists
 Delete the policy you created
Next Steps
Setup your Python Environment
25
See https://learninglabs.cisco.com : Coding 102 Set up your system instructions
A text editor
– Text Wrangler, Notepad++, Sublime Text, PyCharm Community Edition etc.
Python installed on your system
– Preinstalled on many Linux distributions and Mac OS
– Windows may need to install - http://python.org/download.
– You can check to see if it is installed by typing “python” at a command prompt.
Python Requests library
– http://docs.python-requests.org/en/latest/user/install/#install
 Here are some sites that can help you setup your system:
– https://wiki.python.org/moin/BeginnersGuide/NonProgrammers
– http://learnpythonthehardway.org/book/ex0.html
– http://www.codecademy.com/tracks/python
First REST call from Python
26
#import requests library
import requests
#specify URL
url = 'http://Your-API-EM-Controller/api/v0/host’
#Call REST API
response = requests.get(url)
#Print Response
print response.text
Source code file: apic-em1.py
Python Examples
27
Coding 102
– apic-em1.py – simple example to get list of hosts
– apic-em-helloworld.py – “hello world” type example to show list of devices
– learning-lab-basics.py – Retrieves device list and pretty prints JSON
– learning-lab-basics-step2.py – Retrieves network device list and parses JSON
to display networkDeviceId values
– learning-lab-basics-step3.py – Retrieves and lists all devices, hosts, policies
and configured applications
– learning-lab-create-policy.py – Shows how to create a new policy using the
POST Method
https://github.com/CiscoDevNet/coding-skills-sample-code
Download Sample Code
Python Examples
28
Create a Policy –
– Basic steps
– 1. Get Hosts
– 2. Get Policies
– 3. Create a new Policy
– 4. Get Policies again to show new one that was added
Use POST Method
Set Header
– Content-Type: application/json
What if policy already exists?
A unique combination of hostIP, policy name, and ports is required to add a new
policy.
Source code file: learning-lab-create-policy.py
 learninglabs.cisco.com
 Coding 201 – Coding 207
 Next steps with Python
 developer.cisco.com
 Youtube Videos
 Cisco DevNet Github
 Get your DevNet Loot reward points!
Where to go from here?
References
30
 http://learnpythonthehardway.org/book/
 http://docs.python-requests.org/en/latest/index.html
 https://wiki.python.org/moin/BeginnersGuide/NonProgrammers
 http://www.codecademy.com/tracks/python
 https://www.python.org/doc/
 http://www.pythonlearn.com/html-008/cfbook014.html
 http://www.soapui.org/Best-Practices/understanding-rest-headers-and-parameters.html
 http://www.w3schools.com/jQuery/
 http://api.jquery.com/
Coding Classes all Week in the DevNet Zone
https://developer.cisco.com/site/DevNetZone/
Monday Tuesday Wednesday
8-9 am Coding 101 11- 12 pm Coding 203 10-11 am Coding 201
9-10 am Coding 101 1:30 -1 pm Coding 201
Workshop
11- 12 pm Coding 101
12 – 1pm Coding 201 2:30- 3 pm Coding 101
Workshop
11:30 -12 Coding 201
Workshop
1- 2 pm Coding 203 12 -1 pm Coding 210
2:30-3 Coding 101
Workshop
2:30- 3 pm Coding 203
Workshop
2-3 pm Coding 210
About Me
Amanda Whaley
• DevNet Community Manager
• amwhaley@cisco.com
• @mandywhaley
• Lives in Austin, TX.
• developer.cisco.com
• learninglabs.cisco.com
“Those who never fail
are those who never
try.”
-- Ilka Chase
Thank you

More Related Content

PPT
Learn REST API with Python
PDF
Building Automated REST APIs with Python
PPTX
From ZERO to REST in an hour
PDF
A python web service
PDF
Introduction to ACI APIs
PPTX
Aci programmability
PDF
Coding 100-session-slides
Learn REST API with Python
Building Automated REST APIs with Python
From ZERO to REST in an hour
A python web service
Introduction to ACI APIs
Aci programmability
Coding 100-session-slides

What's hot (20)

ODP
REST API Laravel
PDF
Building Awesome APIs with Lumen
PDF
Ruby HTTP clients comparison
PPTX
F# on the Web
PPTX
40+ tips to use Postman more efficiently
PPTX
A Brief History of OWIN
PDF
Building a chatbot – step by step
PDF
Ruby HTTP clients
PDF
Regex Considered Harmful: Use Rosie Pattern Language Instead
PPT
An Introduction to Solr
PPTX
REST API Best Practices & Implementing in Codeigniter
PDF
Understanding and testing restful web services
PPTX
Postman Collection Format v2.0 (pre-draft)
PPT
Ruby Projects and Libraries
PDF
Laravel 5 Annotations: RESTful API routing
PDF
vienna.js - Automatic testing of (RESTful) API documentation
PDF
What's new in Rails 4
KEY
Intro to PSGI and Plack
PPTX
2012: ql.io and Node.js
PDF
Google App Engine With Java And Groovy
REST API Laravel
Building Awesome APIs with Lumen
Ruby HTTP clients comparison
F# on the Web
40+ tips to use Postman more efficiently
A Brief History of OWIN
Building a chatbot – step by step
Ruby HTTP clients
Regex Considered Harmful: Use Rosie Pattern Language Instead
An Introduction to Solr
REST API Best Practices & Implementing in Codeigniter
Understanding and testing restful web services
Postman Collection Format v2.0 (pre-draft)
Ruby Projects and Libraries
Laravel 5 Annotations: RESTful API routing
vienna.js - Automatic testing of (RESTful) API documentation
What's new in Rails 4
Intro to PSGI and Plack
2012: ql.io and Node.js
Google App Engine With Java And Groovy
Ad

Viewers also liked (20)

PPTX
The Basic Introduction of Open vSwitch
PDF
Automating with NX-OS: Let's Get Started!
PDF
Rest api with Python
PDF
Reliable Python REST API (by Volodymyr Hotsyk) - Web Back-End Tech Hangout - ...
PDF
Python RESTful webservices with Python: Flask and Django solutions
PPTX
How to Build Advanced Voice Assistants and Chatbots
PDF
DEVNET-2002 Coding 201: Coding Skills 201: Going Further with REST and Python...
PDF
Developing RESTful Web APIs with Python, Flask and MongoDB
PPTX
JSON and REST
PDF
Introduction to Python
PPTX
Understanding REST APIs in 5 Simple Steps
PDF
Bidirectional Forwarding Detection (BFD)
PDF
Network Mapper (NMAP)
PDF
Open-E DSS V7 Active-Active Load Balanced iSCSI HA Cluster (with bonding)
PDF
Pycon 2008: Python Command-line Tools *Nix
PPTX
Red Hat Storage Day Seattle: Why Software-Defined Storage Matters
PDF
Policy Based Routing (PBR)
PDF
Routing Implementation - Cisco vs. Mikrotik
PPT
Swift Architecture and Practice, by Alex Yang
The Basic Introduction of Open vSwitch
Automating with NX-OS: Let's Get Started!
Rest api with Python
Reliable Python REST API (by Volodymyr Hotsyk) - Web Back-End Tech Hangout - ...
Python RESTful webservices with Python: Flask and Django solutions
How to Build Advanced Voice Assistants and Chatbots
DEVNET-2002 Coding 201: Coding Skills 201: Going Further with REST and Python...
Developing RESTful Web APIs with Python, Flask and MongoDB
JSON and REST
Introduction to Python
Understanding REST APIs in 5 Simple Steps
Bidirectional Forwarding Detection (BFD)
Network Mapper (NMAP)
Open-E DSS V7 Active-Active Load Balanced iSCSI HA Cluster (with bonding)
Pycon 2008: Python Command-line Tools *Nix
Red Hat Storage Day Seattle: Why Software-Defined Storage Matters
Policy Based Routing (PBR)
Routing Implementation - Cisco vs. Mikrotik
Swift Architecture and Practice, by Alex Yang
Ad

Similar to DEVNET-1001 Coding 101: How to Call REST APIs from a REST Client and Python (20)

PPTX
Apic dc api deep dive
PPTX
Cisco APIs: An Interactive Assistant for the Web2Day Developer Conference
PPTX
Advanced Postman for Better APIs - Web Summit 2018 - Cisco DevNet
PPTX
Design Summit - RESTful API Overview - John Hardy
PPTX
DEVNET-1136 Cisco ONE Enterprise Cloud Suite for Infrastructure Management.
PPTX
DevNet 1056 WIT Spark API and Chat Bot Workshop
PPTX
Open Device Programmability: Hands-on Intro to RESTCONF (and a bit of NETCONF)
PDF
Expanding your impact with programmability in the data center
PPTX
Coding 102 REST API Basics Using Spark
PPTX
Understanding APIs.pptx
PPTX
Understanding APIs.pptx introduction chk
PPTX
BEST REST in OpenStack
PDF
Webinar: Applying REST to Network Management – An Implementor’s View
PPTX
Customizing Cisco Collaboration Devices - CL20B - DEVNET-2071
PDF
REST API Basics
PDF
7 network programmability concepts api
PPT
APIC-EM API Deep Dive
PDF
7 network programmability concepts api
PPTX
CWS-206-1I_03_Web and Data Elements_v1.02.pptx
PDF
There is REST and then there is "REST"
Apic dc api deep dive
Cisco APIs: An Interactive Assistant for the Web2Day Developer Conference
Advanced Postman for Better APIs - Web Summit 2018 - Cisco DevNet
Design Summit - RESTful API Overview - John Hardy
DEVNET-1136 Cisco ONE Enterprise Cloud Suite for Infrastructure Management.
DevNet 1056 WIT Spark API and Chat Bot Workshop
Open Device Programmability: Hands-on Intro to RESTCONF (and a bit of NETCONF)
Expanding your impact with programmability in the data center
Coding 102 REST API Basics Using Spark
Understanding APIs.pptx
Understanding APIs.pptx introduction chk
BEST REST in OpenStack
Webinar: Applying REST to Network Management – An Implementor’s View
Customizing Cisco Collaboration Devices - CL20B - DEVNET-2071
REST API Basics
7 network programmability concepts api
APIC-EM API Deep Dive
7 network programmability concepts api
CWS-206-1I_03_Web and Data Elements_v1.02.pptx
There is REST and then there is "REST"

More from Cisco DevNet (20)

PPTX
How to Contribute to Ansible
PPTX
Rome 2017: Building advanced voice assistants and chat bots
PPTX
Cisco Spark and Tropo and the Programmable Web
PPTX
Device Programmability with Cisco Plug-n-Play Solution
PPTX
Building a WiFi Hotspot with NodeJS: Cisco Meraki - ExCap API
PPTX
Application Visibility and Experience through Flexible Netflow
PPTX
WAN Automation Engine API Deep Dive
PPTX
Cisco's Open Device Programmability Strategy: Open Discussion
PPTX
NETCONF & YANG Enablement of Network Devices
PPTX
UCS Management APIs A Technical Deep Dive
PPTX
OpenStack Enabling DevOps
PPTX
NetDevOps for the Network Dude: How to get started with API's, Ansible and Py...
PPTX
Getting Started: Developing Tropo Applications
PPTX
Cisco Spark & Tropo API Workshop
PPTX
DevNet Express - Spark & Tropo API - Lisbon May 2016
PPTX
DevNet @TAG - Spark & Tropo APIs - Milan/Rome May 2016
PDF
Choosing PaaS: Cisco and Open Source Options: an overview
PDF
Doing Business with Tropo
PDF
Introduction to the DevNet Sandbox and IVT
PDF
Introduction to Fog
How to Contribute to Ansible
Rome 2017: Building advanced voice assistants and chat bots
Cisco Spark and Tropo and the Programmable Web
Device Programmability with Cisco Plug-n-Play Solution
Building a WiFi Hotspot with NodeJS: Cisco Meraki - ExCap API
Application Visibility and Experience through Flexible Netflow
WAN Automation Engine API Deep Dive
Cisco's Open Device Programmability Strategy: Open Discussion
NETCONF & YANG Enablement of Network Devices
UCS Management APIs A Technical Deep Dive
OpenStack Enabling DevOps
NetDevOps for the Network Dude: How to get started with API's, Ansible and Py...
Getting Started: Developing Tropo Applications
Cisco Spark & Tropo API Workshop
DevNet Express - Spark & Tropo API - Lisbon May 2016
DevNet @TAG - Spark & Tropo APIs - Milan/Rome May 2016
Choosing PaaS: Cisco and Open Source Options: an overview
Doing Business with Tropo
Introduction to the DevNet Sandbox and IVT
Introduction to Fog

Recently uploaded (20)

PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Empathic Computing: Creating Shared Understanding
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Approach and Philosophy of On baking technology
PPTX
Big Data Technologies - Introduction.pptx
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
KodekX | Application Modernization Development
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PPTX
Cloud computing and distributed systems.
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPT
Teaching material agriculture food technology
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Empathic Computing: Creating Shared Understanding
Advanced methodologies resolving dimensionality complications for autism neur...
Approach and Philosophy of On baking technology
Big Data Technologies - Introduction.pptx
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Unlocking AI with Model Context Protocol (MCP)
Digital-Transformation-Roadmap-for-Companies.pptx
KodekX | Application Modernization Development
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Cloud computing and distributed systems.
Understanding_Digital_Forensics_Presentation.pptx
Per capita expenditure prediction using model stacking based on satellite ima...
Diabetes mellitus diagnosis method based random forest with bat algorithm
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Chapter 3 Spatial Domain Image Processing.pdf
Teaching material agriculture food technology
20250228 LYD VKU AI Blended-Learning.pptx

DEVNET-1001 Coding 101: How to Call REST APIs from a REST Client and Python

  • 1. Coding Skills 101: How to call REST APIs from Python Amanda Whaley – DevNet Community Manager amwhaley@cisco.com @mandywhaley
  • 2. Agenda • Who is this session for? • REST Web Service Basics • Try the APIC-EM APIs from Postman • Python Environment Setup • Python Examples • NEXT UI Toolkit
  • 3. Who is Coding 101 for? New coders Returning coders NetOps DevOps
  • 4. Congratulations! You have learned: 4 • REST Web Service Basics • Anatomy of a REST API Request • GET and POST • How to call the APIC-EM APIs from Postman • How to call REST APIs from Python • Requests Library
  • 5. Disclaimer: Sample Code vs. Real Code 5 The examples and code in this presentation are for Learning and Educational purposes. The samples were created with the goals of clarity and ease of understanding. If you are writing code for a real application, you would write the code in a more efficient and structured style.
  • 6. Follow along with the Learning Lab 6 • Login with DevNet/Cisco.com ID • Coding 101 Lab • Coding 102 Lab • Taking it further: Coding 201-207 + APIC-EM Learning Labs Follow Coding 102 instructions to setup your system • Chrome • A text editor (text wrangler, notepad ++, sublime text etc.) • Postman REST Client -- http://www.getpostman.com/ • Python • Go to command prompt and type python3 – did it work? • http://learnpythonthehardway.org/book/ex0.html • Python Requests library -- http://docs.python-requests.org https://learninglabs.cisco.com
  • 7. DevNet APIC-EM Always On Sandbox 7 Always there for you to use… • Register on Cisco DevNet (http://developer.cisco.com ) to get the latest information about APIs as they are release • You can use the Always-On APIC-EM Sandbox at anytime • You can use https://learninglabs.cisco.com at anytime https://sandboxapic.cisco.com/
  • 8. DevNet GitHub 8 coding-skills-sample-code repository • All the code for this session • All the code for all of the Coding Skills Learning Labs https://github.com/CiscoDevNet
  • 9. So what is a REST Web Service? 9 What is a Web Service? – A way for two systems to communicate through a defined interface. – Two major types of Web Services – REST or SOAP What is a REST Web Service? – REST is an architecture style for designing networked applications. – A REST web service is a web service that is as easy to call as making an HTTP request. – RESTful interfaces often offer the CRUD operations
  • 10. What is so great about REST? 10 • Hosts • Devices • Policies • Applications Easy to use: • In mobile apps • In console apps • In web apps How does this work? Cisco APIC-EM REST APIs
  • 11. How does this work? 8 3rd Party App 3rd Party App Request Response Get Hosts List of Hosts Cisco APIC-EM Cisco APIC-EM
  • 12. APIC-EM Example: Get Hosts 12 3rd Party App GET http://{APIC-EMController}/api/v0/host/{start}/{no. rec} List of Hosts returned in JSON Cisco APIC-EM APIC-EM Learning labs Application Policy Infrastructure Controller (APIC) Enterprise Module (EM) Request Response
  • 13. Anatomy of a REST Request 13 Method – GET, POST, PUT, DELETE URL – Example: http://{APIC-EMController}/api/v0/host/1/3 Authentication – Basic HTTP, OAuth, none Custom Headers – HTTP Headers – Example: Content-Type: application/json Request Body – JSON or XML containing data needed to complete request JSON -- JavaScript Object Notation, is a lightweight text-based open standard designed for human-readable data interchange.
  • 14. Using the API Reference Documentation
  • 15. Using the API Reference Documentation
  • 16. And what is in the Response? 16 HTTP Status Codes – http://www.w3.org/Protocols/HTTP/HTRESP.html – 200 OK – 201 Created – 500 Internal Error Headers Body – JSON – XML
  • 17. REST in Action: How can I try it? 17 HTTP clients can help you quickly test web services  Postman - http://www.getpostman.com/  Firefox RestClient - https://addons.mozilla.org/en-US/firefox/addon/restclient/  Command Line using curl - http://curl.haxx.se/docs/httpscripting.html#GET  SOAPUI Many IDEs have consoles for testing REST Services built in We are going to use Postman as an example.
  • 18. REST Demo – Using Postman 18
  • 19. REST Demo – Using Postman 19
  • 20. REST Demo – Using Postman 20  Get Hosts – Method: GET – URL: http://APIC-EMController/api/v0/host/{startIndex}/{recordsToReturn}  Get Devices – Method: GET – URL: http://APIC-EMController/api/v0/network-device/{startIndex}/{recordsToReturn}  Get Policies – Method: GET – URL: http://APIC-EMController/api/v0/policy/{startIndex}/{recordsToReturn}  Get Applications – Method: GET – URL: http://APIC-EMController/api/v0/application/{startIndex}/{recordsToReturn}
  • 21. REST DEMO – Using the POST or PUT Method 21 To send data to a REST service and either create or update data, you will need to use POST or PUT. Create Policy Example – Method: POST – URL: http://APIC-EMController/api/v0/policy – Custom Headers: Content-Type: application/json – Request Body: JSON that specifies details of new policy What if the Content-Type header is missing? What if there is a mistake in the JSON Request Body? Handy tool for validating JSON -- http://jsonlint.com/
  • 22. REST Demo – Using POST or PUT Method 22
  • 23. If you didn't receive a 202 Accepted message, check the following:  Is your URI correct?  Did you choose the method to be Post?  Did you remember to specify the Content-Type header as application/json?  Did you get a message indicating that the policy already exists?  If so, try it again with a different port (please use only high ports) or host. Wait that didn’t work!??!
  • 24.  Verify that your new policy exists  Delete the policy you created Next Steps
  • 25. Setup your Python Environment 25 See https://learninglabs.cisco.com : Coding 102 Set up your system instructions A text editor – Text Wrangler, Notepad++, Sublime Text, PyCharm Community Edition etc. Python installed on your system – Preinstalled on many Linux distributions and Mac OS – Windows may need to install - http://python.org/download. – You can check to see if it is installed by typing “python” at a command prompt. Python Requests library – http://docs.python-requests.org/en/latest/user/install/#install  Here are some sites that can help you setup your system: – https://wiki.python.org/moin/BeginnersGuide/NonProgrammers – http://learnpythonthehardway.org/book/ex0.html – http://www.codecademy.com/tracks/python
  • 26. First REST call from Python 26 #import requests library import requests #specify URL url = 'http://Your-API-EM-Controller/api/v0/host’ #Call REST API response = requests.get(url) #Print Response print response.text Source code file: apic-em1.py
  • 27. Python Examples 27 Coding 102 – apic-em1.py – simple example to get list of hosts – apic-em-helloworld.py – “hello world” type example to show list of devices – learning-lab-basics.py – Retrieves device list and pretty prints JSON – learning-lab-basics-step2.py – Retrieves network device list and parses JSON to display networkDeviceId values – learning-lab-basics-step3.py – Retrieves and lists all devices, hosts, policies and configured applications – learning-lab-create-policy.py – Shows how to create a new policy using the POST Method https://github.com/CiscoDevNet/coding-skills-sample-code Download Sample Code
  • 28. Python Examples 28 Create a Policy – – Basic steps – 1. Get Hosts – 2. Get Policies – 3. Create a new Policy – 4. Get Policies again to show new one that was added Use POST Method Set Header – Content-Type: application/json What if policy already exists? A unique combination of hostIP, policy name, and ports is required to add a new policy. Source code file: learning-lab-create-policy.py
  • 29.  learninglabs.cisco.com  Coding 201 – Coding 207  Next steps with Python  developer.cisco.com  Youtube Videos  Cisco DevNet Github  Get your DevNet Loot reward points! Where to go from here?
  • 30. References 30  http://learnpythonthehardway.org/book/  http://docs.python-requests.org/en/latest/index.html  https://wiki.python.org/moin/BeginnersGuide/NonProgrammers  http://www.codecademy.com/tracks/python  https://www.python.org/doc/  http://www.pythonlearn.com/html-008/cfbook014.html  http://www.soapui.org/Best-Practices/understanding-rest-headers-and-parameters.html  http://www.w3schools.com/jQuery/  http://api.jquery.com/
  • 31. Coding Classes all Week in the DevNet Zone https://developer.cisco.com/site/DevNetZone/ Monday Tuesday Wednesday 8-9 am Coding 101 11- 12 pm Coding 203 10-11 am Coding 201 9-10 am Coding 101 1:30 -1 pm Coding 201 Workshop 11- 12 pm Coding 101 12 – 1pm Coding 201 2:30- 3 pm Coding 101 Workshop 11:30 -12 Coding 201 Workshop 1- 2 pm Coding 203 12 -1 pm Coding 210 2:30-3 Coding 101 Workshop 2:30- 3 pm Coding 203 Workshop 2-3 pm Coding 210
  • 32. About Me Amanda Whaley • DevNet Community Manager • amwhaley@cisco.com • @mandywhaley • Lives in Austin, TX. • developer.cisco.com • learninglabs.cisco.com “Those who never fail are those who never try.” -- Ilka Chase

Editor's Notes

  • #10: REST stands for Representational State Transfer. It relies on a stateless, client-server, cacheable communications protocol (ex. HTTP)
  • #14: http://www.json.org/
  • #23: Show what happens if you forget the Content-Type Header
  • #27: More info on requests library - http://docs.python-requests.org/en/latest/index.html