REST APIs Part 1 - HTTP Is For More Than Web Browsing PDF
REST APIs Part 1 - HTTP Is For More Than Web Browsing PDF
Network Programmability Basics/Programming Fundamentals/REST APIs Part 1: HTTP is for more than Web Browsing
Network Programmability Basics Modules
• Introduction: How to be a Network Engineer in a Programmable Age
• Programming Fundamentals
• Network Device APIs
• Network Controllers
• Application Hosting and the Network
• NetDevOps
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Network Programmability Basics: The Lessons
Module: Programming Fundamentals
• Data Formats: Understanding and using JSON, XML and YAML
• APIs are Everywhere... but what are they?
• REST APIs Part 1: HTTP is for more than Web Browsing
• REST APIs Part 2: Making REST API Calls with Postman
• Python Part 1: Python Language and Script Basics
• Python Part 2: Working with Libraries and Virtual Environments
• Python Part 3: Useful Python Libraries for Network Engineers
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Code and Develop Along
• Get the Code!
• github.com/CiscoDevNet/netprog_basics
• Setup Lab Prerequisites
• Each lab includes a README with details
• Access to Infrastructure
• DevNet Sandbox
• Specifics in lab README
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Topics to Cover
• What is REST?
• A Look Under the Hood at
REST?
• Some REST Examples
• REST API Tools
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
What is REST?
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Just Another Use for the HTTP Protocol
• Representational state transfer
(REST)
• API framework built on HTTP
• APIs often referred to as web
services
• Popular due to performance,
scale, simplicity, and reliability
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Requests and Response, the REST API Flow
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Requests and Response, the REST API Flow
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Requests and Response, the REST API Flow
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
A Look Under the Hood at
REST?
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
The URI: What are you Requesting?
http://maps.googleapis.com/maps/api/geocode/json?address=sanjose
Server or Host Resource Parameters
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
HTTP Methods: What to do?
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Response Status Codes: Did it work?
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Response Status Codes: Did it work?
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Headers: Details and meta-data
• Some APIs will use custom headers for authentication or other purpose
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Data: Sending and Receiving
• Contained in the body • {
• 'title': 'Hamlet',
• POST, PUT, PATCH requests • 'author': 'Shakespeare'
}
typically include data
•
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
HTTP Authentication and Security
• None: the Web API resource is public, anybody can place call.
• Basic HTTP: a username and password are passed to the server in an
encoded string.
• Authorization: Basic ENCODEDSTRING
• Token: a secret generally retrieved from the Web API developer portal.
Keyword (ie token) is API dependent
• Authorization: Token aikasf8adf9asd9akasdf0asd
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
The Internet Chuck Norris Database
• DevNet$ curl https://api.icndb.com/jokes/random
• {
• "type": "success",
• "value": {
• "id": 201,
• "joke": "Chuck Norris was what Willis was talkin' about.",
• "categories": []
• }
• }
• {
• "type": "success",
• "value": {
• "id": 537,
• "joke": "Each hair in Chuck Norris's beard contributes to make the world's largest DDOS.",
• "categories": [
• "nerdy"
• ] • http://www.icndb.com/api/
• }
• } • No authentication needed
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Confidential • Well constructed API with many options
Network Programmability with RESTCONF
The Request
• DevNet$ curl -vk \
• -u root:D_Vay\!_10\& \
• -H 'accept: application/yang-data+json' \
• https://ios-xe-mgmt.cisco.com:9443/restconf/data/ietf-interfaces:interfaces/interface=GigabitEthernet2
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Demo Time!
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
REST API Tools
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Many Options for Working with REST APIs
• curl
• Linux command line application
• Postman
• Chrome browser plugin and application
• Requests
• Python library for scripting
• Swagger
• Dynamic API Documentation
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Summing up
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Review
• REST APIs are built on the HTTP Protocol
• Requests and Responses
• How are URIs constructed
• Methods, Status Codes, and Headers used with REST APIs
• Authentication options for HTTP
• Looked at some example API calls
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Call to Action!
• Complete the full Network
Programmability Basics Course
• Run the examples and
exercises yourself!
• Bonus Examples!
• Join DevNet for so much more!
• Learning Labs
• Development Sandboxes
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Got more questions? Come find me!
[email protected]
@hfpreston
http://github.com/hpreston
@CiscoDevNet
facebook.com/ciscodevnet/
http://github.com/CiscoDevNet
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Confidential