O'Reilly logo
live online training icon Live Online training

Getting Started with Python Requests Using REST APIs

Topic: Software Development
Nicholas Russo

A practical and demonstration-oriented course covering the popular HTTP client “requests” for Python in the context of REST APIs and other business use-cases. Python is among the most commonly-used programming languages today. REST APIs built atop HTTP are almost ubiquitous in modern business applications. This course combines these two popular topics by teaching you how to use Python “requests,” a simple yet powerful HTTP client library. Whether you are designing a microservice application that utilizes API calls between components or trying to automate your IT infrastructure using HTTP-based APIs, this course is for you.

Instructor Nicholas Russo's publicly available Python projects utilizing the “requests” library will be evaluated throughout this course to provide real-life context and demonstrate the value it brings to any organization. This course has very little static content and is primarily taught using hands-on demonstrations, making it different from other forms of training. This training expects that participants have some Python programming experience. On a scale of 1-10, with 10 being an expert, participants should have a skill level of at least 3.

What you'll learn-and how you can apply it

  • Brief HTTP and REST refreshers (but not enough to skip the prerequisites listed)
  • Implementing/processing common HTTP actions such as pagination and cache control
  • Create, read, update, and delete (CRUD) operations on public REST APIs
  • Applying “requests” to real-life situations from the author’s personal business

This training course is for you because...

  • Training is for Software Developers, IT Operations Engineers of all kinds (Network Engineers, Systems Administrators, Database Administrators, etc), Automation/Test Engineers, or any other role that requires programmatic and automation interaction with HTTP servers
  • Course is intermediate level of difficulty (See Prerequisites)
  • Course is primarily focused on Python programming as a technology. However, the modern world requires engineers to have some “business smarts.” The instructor always ties in “what” we are doing to “why” it matters for businesses and their tolerance for risk.

Prerequisites

  • Basic Linux/MacOS knowledge, such as shell familiarity and text editing.
  • Basic Python and REST knowledge, such as writing simple scripts to interact with existing REST APIs
  • Understanding of common data formats such as HTML and JSON

If you'd like to learn by watching the instructor's demonstrations, no setup is required. These setup steps are optional:

Recommended Preparation

Recommended Follow-up

About your instructor

  • Nicholas Russo holds a Bachelor’s of Science in Computer Science from the Rochester Institute of Technology (RIT). His primary programming experience is in C# and Java, but has been programming almost exclusively in Ansible and Python over the past several years. Nick’s day job is an automation-focused network engineer but he loves teaching others about technology. Nick lives in Maryland, USA with his wife, Carla, and daughter, Olivia.

Schedule

The timeframes are only estimates and may vary according to how the class is progressing

HTTP Refresher Length (10 min)

  • Instructor will quickly review common HTTP operations to level-set all attendees. This includes request/response flow and components, status codes, and common methods.
  • Participants will immediately know whether this is the right course for them. If all the prequisities are complete, then this should be a refresher. For those less familiar with HTTP, this section should reinforce the core concepts upon which the rest of this course is based.

Introducing Python requests Length (40 min)

  • Instructor will introduce the requests package and some of the most common uses of it, such as specific methods, URLs, and headers. This also includes package installation and several small tests using the Python REPL (interactive shell) and script files. For a deeper look into how requests work, instructor will set up basic Python logging to reveal individual HTTP exchanges between client and server.
  • Participants will get their first glimpse into the functionality of “requests” and how to get started using simple examples. These techniques will lay the foundation for continued learning and troubleshooting later in the course.

Break (length: 10 min)

Additional requests Capabilities Length (25 min)

  • Instructor will explain how HTTP caching works and why this is commonly seen in real life, especially for large files/digital entertainment. This includes a discussion on long-lived “sessions” for increased performance. Additionally, the instructor will detail the nuanced difference between “data” and “json” as it relates to HTTP body payloads.
  • Participants will see additional examples using real websites/resources illustrating the value of Python requests in collecting information, authenticating to APIs, etc.

REST Refresher and API testing Length (25 min)

  • Instructor will quickly review the REST architectural style and why it has become so popular. This includes a brief demo to highlight interacting with a publicly available REST API.
  • If all the prerequisites are complete, then this should be a refresher. For those less familiar with REST, this section should reinforce the core REST concepts.

Break (length: 10 min)

Interacting with REST APIs Length (25 min)

  • Instructor will use a mix of Python REPL (interactive shell) and script files to interact with a variety of publicly accessible, free REST APIs. This includes create, read, update, and delete (CRUD) functionality where applicable. These examples will exercise a variety of HTTP client techniques that demonstrate the capabilities of the “requests” library, such as error handling, timeouts, pagination, query parameters, and body payloads.
  • Participants will gain valuable experience in working with REST APIs if they choose to follow along. The scripts reviewed in this section can serve as templates or starting points for participants for use in their own projects. 

Code review: tweeter-lite Length (25 min)

  • Instructor will review the tweeter-lite project (linked in Course Set-Up). This project loads in Twitter messages (called “tweets”) and validates the URL contained within it. This has business relevance as it allows individuals to maintain a library of known-good tweets, allowing for the URLs to be re-validated before publication. This section also focuses on simpler, more generic deep-dives into HTTP redirection and how you can manipulate “requests” to handle them differently.
  • Participants will understand how to utilize “requests” for simple business tasks such as web crawling, link validation, redirection handling, and more. This includes automatic or manual redirect following and optional security checks.

Break (length: 10 min)

Code review: py-auphonic Length (55 min)

  • Instructor will review the py-auphonic project (linked in Course Set-Up). This project interacts with the Auphonic API using object-oriented programming, a stateful HTTP session, and various methods to encapsulate the “requests” library. It is an example of a professional-grade, real-world business application.
  • Participants will understand how to extend “requests” using composition via Python classes and how to scalably create test scripts using the public SDK.

Recap / Q&A Length (5 min)

  • Instructor will answer questions and provide additional resources/advice for future studies.
  • Participants will have an opportunity to clarify any points of confusion or discuss difficult technical topics from the course.