By the end of this lesson, you should be able to...
requests library to make simple API requests and display the response.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
<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:
requests.get(routeURL, params) = the Requests method used to create a GET request
routeURL = a string with the route URL information required to find the desired serverparams = an object of key-value pairs that contains the query parameters of the search.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()