When making API requests, you'll often need to send parameters along with the request URL to filter data, paginate results, or authenticate the request. The Python Requests library makes this easy by allowing you to pass a dictionary of parameters.
Here's an example GET request that passes two parameters,
import requests
url = 'https://api.example.com/products'
params = {
'category': 'electronics',
'page': 2
}
response = requests.get(url, params=params)
This will make a request to something like
The
You can also pass a list of items as a parameter value to represent multiple values for the same key:
params = {
'category': 'electronics',
'tags': ['on-sale', 'clearance']
}
This would produce a URL like:
When passing sensitive information like API keys, you'll want to use the
headers = {
'Authorization': 'Bearer <api_key>'
}
response = requests.get(url, headers=headers)
The Requests library handles all the intricacies of encoding and building request URLs for you. By passing parameters as a dictionary, you keep your code simple while still being able to generate complex requests.