WEB 1.1 - Module 3: Lesson 5

Learning Outcomes 💫

By the end of this lesson, you should be able to...

Videos 🎥

Vid 1 - walking through how to create requests via the Requests Python library

Vid 1 - walking through how to create requests via the Requests Python library

Exercises 💪

  1. Complete the exercises in this repl.it (only API, no Flask) and submit your work.
  2. Complete the exercises in this repl.it (API and Flask) and submit your work.

Written Companion 🗒

<aside> 🤔 We can use tools like Postman to quickly make requests for API testing, but how do we create a request in Python?

</aside>


When developing in a Python environement, the Requests library can be used to create requests. The Requests library provides us with the following tools:

The results of a requests.get() method includes information such as the header, packet data, and payload. Typically, an application only needs the payload data (most commonly a JSON object) so developers will usually use the .json() method on the results to seperate the valuable information from the junk.

import requests

# an object containing all the key-value pairs relevant to the query
params = { "limitTo": "nerdy" } 

# result contains the respone recieved from the requests.get() method
result = requests.get(
    "<http://api.icndb.com/jokes/random>", 
    params=params) 

# strip the JSON data from the results for easier access
joke_json = result.json()