Here's an example of calling an API in Python using the requests
library. This example assumes that the API returns JSON data.
main.py410 chars16 lines
In this example, we first import the requests
library, which allows us to make HTTP requests. We then make a GET request to the API using the get()
method of the requests
module. The URL of the API is passed as an argument to the get()
method.
We then check the status code of the response using the status_code
attribute of the Response object. If the status code is 200 (which indicates success), we parse the JSON data from the response using the json()
method of the Response object.
Finally, we do something with the data (in this example, we just print it). If the status code indicates an error, we handle the error appropriately (in this example, we just print an error message).
gistlibby LogSnag