SlideShare a Scribd company logo
8
Most read
12
Most read
15
Most read
Larry cai <larry.caiyu@gmail.com>
Agenda








What is REST API ?
Exercise 1: use curl to play with REST API
Exercise 2: use requests module to GET headers
Exercise 3: write in python script
Exercise 4: HTTPS & Basic Authentication towards Github
Exercise 5: POST your script into gist
Summary & Homework & Reference

2

Learn REST API with Python in 90 minutes
REST API



REST = REpresentation State Transfer
REST vs. SOAP
XML vs. JSON



REST/JSON is first choice to use ! (twitter, jenkins…)




See slides http://www.slideshare.net/rmaclean/json-and-rest for detail
Or
"Beautiful REST + JSON APIs" by Les Hazlewood
http://www.youtube.com/watch?v=hdSrT4yjS1g

3

Learn REST API with Python in 90 minutes
Environment


Python 2.7.x In Windows with Git Bash




4

http://www.python.org/ftp/python/2.7.3/python-2.7.3.msi
http://git-scm.com/downloads
Add into Path

Learn REST API with Python in 90 minutes
Exercise 1: using curl


Try to get the data in browser and command using cURL
http://httpbin.org/get

$ export http_proxy=
$ export https_proxy=
$ curl –v http://httpbin.org/get

5

Learn REST API with Python in 90 minutes
Exercise: use urllib2 module



SKIP this, it wastes time

6

Learn REST API with Python in 90 minutes
Requests Module




Python’s standard urllib2 module provides most of the
HTTP capabilities you need, but the API is
thoroughly broken.
Python HTTP: When in doubt, or when not in doubt, use
Requests. Beautiful, simple, Pythonic.

http://www.python-requests.org

7

Learn REST API with Python in 90 minutes
Exercise 2: use requests module


Install requests module (could use pip)
http://docs.python-requests.org/en/latest/user/install/#install



Use it interactive mode
$ python
>>> import requests
>>> r = requests.get("http://httpbin.org/get")

8

Learn REST API with Python in 90 minutes
JSON format and usage in python


JSON (JavaScript Object Notation) is a lightweight datainterchange format. It is easy for humans to read and
write. It is easy for machines to parse and generate.



>>> print r.json()



Python JSON module
>>>import json
>>>data[“firstName”]
“John”

http://www.slideshare.net/rmaclean/json-and-rest
9

Learn REST API with Python in 90 minutes
Exercise 3: write in python script


Download exercise code restapi.py from github
NOTE: this is prepared script




https://gist.github.com/larrycai/7823499

Implement tasks in method exer3()


print result text, status_code for http://httpbin.org/get
print r.status_code






Print “origin” and “User-Agent” (JSON) (use r.json())
Enable debug by calling enable_debug()

Execute command to verify
$ ./restapi.py –e 3

10

Learn REST API with Python in 90 minutes
Exercise 4: HTTPS & Authentication


HTTPS (SSL certification)
$ curl https://api.github.com/users/larrycai
=> requests.get(…,verify=False)



Authentication (Basic, Digest, OAuth), use curl command
$ curl –v –u larrycai https://api.github.com/user
> Authorization: Basic bGFycnljYWk6TTFkZUBMMWZl
=> HTTP Basic Auth => requests.get(…,auth = HTTPBasicAuth(user, passwd))



Tasks:



Use curl to try your own account
Use script get your account’s “created_at”

http://developer.github.com/v3/

11

Learn REST API with Python in 90 minutes
Exercise 5: POST data


Upload with POST method
curl -X POST -d
'{"public":true,"files":
{"test.txt":{"content":"String
file contents"}}}'
https://api.github.com/gists



>>> import json
>>> json.dumps(['foo', {'bar':
('baz', 1.0, 2)}])
'["foo", {"bar": ["baz",1.0, 2]}]'
>>> print json.dumps(""foobar")
""foobar“



Task:


Upload your current restapi.py
to YOUR gist
http://developer.github.com/v3/gists/#create-a-gist

12

Learn REST API with Python in 90 minutes
Summary



Requests module is preferred to use in python
Try browser/cURL before script to access API
REST/JSON is good marriage



Coding and learn from others




13

Learn REST API with Python in 90 minutes
Exercise 6 : Homework


Implement for all gist API



Use oauth to access github in python script



Get all jenkins jobs in python script



Submit your final result into gist, then give me email, you
may get gift ;-)

14

Learn REST API with Python in 90 minutes
Reference




Understand REST API: http://www.restapitutorial.com/
Requests python module: http://www.python-requests.org
Github developer API v3




http://developer.github.com/v3/

Jenkins API


15

https://wiki.jenkins-ci.org/display/JENKINS/Remote+access+API

Jenkins Scriptler in 90 minutes

More Related Content

PPTX
Automate using Python
PDF
Rest api with Python
PDF
Python/Django Training
PDF
Web develop in flask
PPTX
Django - Python MVC Framework
PDF
Python Full Stack Developer Course Syllabus.pdf
PDF
Python Projects For Beginners | Python Projects Examples | Python Tutorial | ...
PDF
Django interview Questions| Edureka
Automate using Python
Rest api with Python
Python/Django Training
Web develop in flask
Django - Python MVC Framework
Python Full Stack Developer Course Syllabus.pdf
Python Projects For Beginners | Python Projects Examples | Python Tutorial | ...
Django interview Questions| Edureka

What's hot (20)

PPTX
Python SQite3 database Tutorial | SQlite Database
PPTX
Introduction to Node.js
PPT
HTTP Basics
PDF
What is Python JSON | Edureka
PPT
JQuery introduction
PDF
A Basic Django Introduction
PDF
Introduction to django framework
PPTX
graphics programming in java
PPTX
Web Scraping using Python | Web Screen Scraping
PPTX
Basic concepts for python web development
PDF
Python Interview Questions And Answers 2019 | Edureka
PPTX
File handling in Python
PPTX
Basics of MongoDB
PPT
Spring data presentation
KEY
Web API Basics
PPTX
Introduction to Spring Boot
PPTX
JSON: The Basics
PDF
ORM: Object-relational mapping
PPTX
Introduction to numpy Session 1
Python SQite3 database Tutorial | SQlite Database
Introduction to Node.js
HTTP Basics
What is Python JSON | Edureka
JQuery introduction
A Basic Django Introduction
Introduction to django framework
graphics programming in java
Web Scraping using Python | Web Screen Scraping
Basic concepts for python web development
Python Interview Questions And Answers 2019 | Edureka
File handling in Python
Basics of MongoDB
Spring data presentation
Web API Basics
Introduction to Spring Boot
JSON: The Basics
ORM: Object-relational mapping
Introduction to numpy Session 1
Ad

Similar to Learn REST API with Python (20)

PDF
Fun! with the Twitter API
PDF
Building RESTful APIs
PDF
Automating Cloud Operations: Everything You Wanted to Know about cURL and REST
PDF
REST API Basics
PPTX
Jones "Working with Scholarly APIs: A NISO Training Series, Session One: Foun...
PDF
Reliable Python REST API (by Volodymyr Hotsyk) - Web Back-End Tech Hangout - ...
PDF
Building Automated REST APIs with Python
PPTX
Rest Essentials
PPTX
Understanding APIs.pptx
PPTX
Understanding APIs.pptx introduction chk
PPTX
vogler good section Presentation.pptx
PPTX
Documenting REST APIs
PDF
Why should I care about REST?
PPTX
From ZERO to REST in an hour
PDF
[GDSC-ADYPU] APIs 101 with Postman
PDF
API 101 event.pdf
PPTX
API Testing Using REST Assured with TestNG
PPT
Intro to CloudStack API
PPTX
rest-api-basics.pptx
PDF
Python Ireland Nov 2010 - RESTing with Django
Fun! with the Twitter API
Building RESTful APIs
Automating Cloud Operations: Everything You Wanted to Know about cURL and REST
REST API Basics
Jones "Working with Scholarly APIs: A NISO Training Series, Session One: Foun...
Reliable Python REST API (by Volodymyr Hotsyk) - Web Back-End Tech Hangout - ...
Building Automated REST APIs with Python
Rest Essentials
Understanding APIs.pptx
Understanding APIs.pptx introduction chk
vogler good section Presentation.pptx
Documenting REST APIs
Why should I care about REST?
From ZERO to REST in an hour
[GDSC-ADYPU] APIs 101 with Postman
API 101 event.pdf
API Testing Using REST Assured with TestNG
Intro to CloudStack API
rest-api-basics.pptx
Python Ireland Nov 2010 - RESTing with Django
Ad

More from Larry Cai (20)

PPTX
Learn kubernetes in 90 minutes
PPT
Learn jobDSL for Jenkins
PPT
Learn RabbitMQ with Python in 90mins
PPT
Learn flask in 90mins
PPT
Learn ELK in docker
PPT
Software Engineer Talk
PPTX
Learn nginx in 90mins
PPT
Learn basic ansible using docker
PPT
Build service with_docker_in_90mins
PPTX
Learn docker in 90 minutes
PPT
Learn Dashing Widget in 90 minutes
PPT
Jenkins Scriptler in 90mins
PPT
Python virtualenv & pip in 90 minutes
PPT
Lead changes in software development
PPT
Python in 90mins
PDF
Practical way to experience of Specification by Example
PPT
Experience from specification_by_examples
PPT
Write book in markdown
PPT
Continuous Integration Introduction
PDF
Agile & ALM tools
Learn kubernetes in 90 minutes
Learn jobDSL for Jenkins
Learn RabbitMQ with Python in 90mins
Learn flask in 90mins
Learn ELK in docker
Software Engineer Talk
Learn nginx in 90mins
Learn basic ansible using docker
Build service with_docker_in_90mins
Learn docker in 90 minutes
Learn Dashing Widget in 90 minutes
Jenkins Scriptler in 90mins
Python virtualenv & pip in 90 minutes
Lead changes in software development
Python in 90mins
Practical way to experience of Specification by Example
Experience from specification_by_examples
Write book in markdown
Continuous Integration Introduction
Agile & ALM tools

Recently uploaded (20)

PDF
Empathic Computing: Creating Shared Understanding
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
cuic standard and advanced reporting.pdf
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Machine learning based COVID-19 study performance prediction
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Modernizing your data center with Dell and AMD
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PPTX
Cloud computing and distributed systems.
PPTX
MYSQL Presentation for SQL database connectivity
Empathic Computing: Creating Shared Understanding
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
cuic standard and advanced reporting.pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Mobile App Security Testing_ A Comprehensive Guide.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
Digital-Transformation-Roadmap-for-Companies.pptx
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Machine learning based COVID-19 study performance prediction
Unlocking AI with Model Context Protocol (MCP)
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
The AUB Centre for AI in Media Proposal.docx
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Modernizing your data center with Dell and AMD
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Cloud computing and distributed systems.
MYSQL Presentation for SQL database connectivity

Learn REST API with Python

  • 2. Agenda        What is REST API ? Exercise 1: use curl to play with REST API Exercise 2: use requests module to GET headers Exercise 3: write in python script Exercise 4: HTTPS & Basic Authentication towards Github Exercise 5: POST your script into gist Summary & Homework & Reference 2 Learn REST API with Python in 90 minutes
  • 3. REST API  REST = REpresentation State Transfer REST vs. SOAP XML vs. JSON  REST/JSON is first choice to use ! (twitter, jenkins…)   See slides http://www.slideshare.net/rmaclean/json-and-rest for detail Or "Beautiful REST + JSON APIs" by Les Hazlewood http://www.youtube.com/watch?v=hdSrT4yjS1g 3 Learn REST API with Python in 90 minutes
  • 4. Environment  Python 2.7.x In Windows with Git Bash    4 http://www.python.org/ftp/python/2.7.3/python-2.7.3.msi http://git-scm.com/downloads Add into Path Learn REST API with Python in 90 minutes
  • 5. Exercise 1: using curl  Try to get the data in browser and command using cURL http://httpbin.org/get $ export http_proxy= $ export https_proxy= $ curl –v http://httpbin.org/get 5 Learn REST API with Python in 90 minutes
  • 6. Exercise: use urllib2 module  SKIP this, it wastes time 6 Learn REST API with Python in 90 minutes
  • 7. Requests Module   Python’s standard urllib2 module provides most of the HTTP capabilities you need, but the API is thoroughly broken. Python HTTP: When in doubt, or when not in doubt, use Requests. Beautiful, simple, Pythonic. http://www.python-requests.org 7 Learn REST API with Python in 90 minutes
  • 8. Exercise 2: use requests module  Install requests module (could use pip) http://docs.python-requests.org/en/latest/user/install/#install  Use it interactive mode $ python >>> import requests >>> r = requests.get("http://httpbin.org/get") 8 Learn REST API with Python in 90 minutes
  • 9. JSON format and usage in python  JSON (JavaScript Object Notation) is a lightweight datainterchange format. It is easy for humans to read and write. It is easy for machines to parse and generate.  >>> print r.json()  Python JSON module >>>import json >>>data[“firstName”] “John” http://www.slideshare.net/rmaclean/json-and-rest 9 Learn REST API with Python in 90 minutes
  • 10. Exercise 3: write in python script  Download exercise code restapi.py from github NOTE: this is prepared script   https://gist.github.com/larrycai/7823499 Implement tasks in method exer3()  print result text, status_code for http://httpbin.org/get print r.status_code    Print “origin” and “User-Agent” (JSON) (use r.json()) Enable debug by calling enable_debug() Execute command to verify $ ./restapi.py –e 3 10 Learn REST API with Python in 90 minutes
  • 11. Exercise 4: HTTPS & Authentication  HTTPS (SSL certification) $ curl https://api.github.com/users/larrycai => requests.get(…,verify=False)  Authentication (Basic, Digest, OAuth), use curl command $ curl –v –u larrycai https://api.github.com/user > Authorization: Basic bGFycnljYWk6TTFkZUBMMWZl => HTTP Basic Auth => requests.get(…,auth = HTTPBasicAuth(user, passwd))  Tasks:   Use curl to try your own account Use script get your account’s “created_at” http://developer.github.com/v3/ 11 Learn REST API with Python in 90 minutes
  • 12. Exercise 5: POST data  Upload with POST method curl -X POST -d '{"public":true,"files": {"test.txt":{"content":"String file contents"}}}' https://api.github.com/gists  >>> import json >>> json.dumps(['foo', {'bar': ('baz', 1.0, 2)}]) '["foo", {"bar": ["baz",1.0, 2]}]' >>> print json.dumps(""foobar") ""foobar“  Task:  Upload your current restapi.py to YOUR gist http://developer.github.com/v3/gists/#create-a-gist 12 Learn REST API with Python in 90 minutes
  • 13. Summary  Requests module is preferred to use in python Try browser/cURL before script to access API REST/JSON is good marriage  Coding and learn from others   13 Learn REST API with Python in 90 minutes
  • 14. Exercise 6 : Homework  Implement for all gist API  Use oauth to access github in python script  Get all jenkins jobs in python script  Submit your final result into gist, then give me email, you may get gift ;-) 14 Learn REST API with Python in 90 minutes
  • 15. Reference    Understand REST API: http://www.restapitutorial.com/ Requests python module: http://www.python-requests.org Github developer API v3   http://developer.github.com/v3/ Jenkins API  15 https://wiki.jenkins-ci.org/display/JENKINS/Remote+access+API Jenkins Scriptler in 90 minutes