make an external api call in python

To make an external API call in Python, we can use the requests library which allows us to send HTTP/1.1 requests. This library provides a simple interface to make different types of requests like GET, POST, PUT, DELETE, etc.

Here's a basic example:

main.py
import requests
import json

response = requests.get("https://jsonplaceholder.typicode.com/todos/1")
data = json.loads(response.text)

print(data)
147 chars
8 lines

In this example, we're making a GET request to the JSONPlaceholder API and retrieving the first todo item. We then parse the JSON response using the json library and print the data to the console.

You can also include parameters, headers, and authentication information in your requests. Check out the requests documentation for more information and advanced usage.

related categories

gistlibby LogSnag