23. Working with APIs and Web Requests
What Is an API?
An API (Application Programming Interface) is a way for different software applications to communicate. Web APIs allow your Python program to interact with external services and fetch or send data over the internet.
Installing the Requests Library
The `requests` library is the most popular Python library for working with HTTP requests. Install it using pip if you haven't already.
pip install requestsMaking a Simple GET Request
GET requests retrieve data from a server. Here's an example fetching a JSON placeholder API.
import requests
response = requests.get('https://jsonplaceholder.typicode.com/posts/1')
if response.status_code == 200:
data = response.json()
print(data)
else:
print('Failed to fetch data')GET Requests with Parameters
You can pass query parameters using the `params` argument to filter or specify data.
params = {'userId': 1}
response = requests.get('https://jsonplaceholder.typicode.com/posts', params=params)
data = response.json()
print(data)POST Requests
POST requests send data to a server, often used to create new resources.
payload = {'title': 'Hello', 'body': 'This is a test', 'userId': 1}
response = requests.post('https://jsonplaceholder.typicode.com/posts', json=payload)
print(response.status_code)
print(response.json())Handling Headers and Authentication
APIs may require headers or authentication tokens. You can include them in your request like this:
headers = {'Authorization': 'Bearer YOUR_TOKEN_HERE'}
response = requests.get('https://api.example.com/data', headers=headers)Parsing JSON Data
Most APIs return data in JSON format. Use `.json()` to convert the response into a Python dictionary or list.
data = response.json()
print(data['title'])Error Handling and Best Practices
- Always check `response.status_code` before processing data.
- Use `try/except` blocks to handle connection errors or timeouts.
- Set timeouts to prevent your code from hanging indefinitely: `requests.get(url, timeout=5)`
- Respect API rate limits to avoid being blocked.
try:
response = requests.get('https://jsonplaceholder.typicode.com/posts', timeout=5)
response.raise_for_status()
data = response.json()
except requests.exceptions.HTTPError as errh:
print('HTTP Error:', errh)
except requests.exceptions.ConnectionError as errc:
print('Connection Error:', errc)
except requests.exceptions.Timeout as errt:
print('Timeout Error:', errt)
except requests.exceptions.RequestException as err:
print('Something went wrong:', err)Mini Project Step
Fetch posts from the JSONPlaceholder API, filter them by `userId`, and display the title and body in a nicely formatted output. Bonus: use a POST request to send a new post to the API.