How to Add Query Parameters to API Calls with requests in Python

How to Add Query Parameters to API Calls with requests in Python

🐍 Python Tip! Easily add query parameters to your API calls when using Python by using the params argument in requests.get().

Here`s an example 👇

import requests

# Define API endpoint
url = "http://suzieq-server/api/v2/devices"

# Query parameters to filter the API request
params = {
    "namespace": "lab",   
    "hostname": "leaf01"   
}

# Make a GET request with query parameters using the 'params' argument
response = requests.get(url, params=params)

# Print the full URL after query parameters have been added
print(response.url)

# Output:
# http://suzieq-server/api/v2/devices?namespace=lab&hostname=leaf01

Subscribe to our newsletter and stay updated.

Don't miss anything. Get all the latest posts delivered straight to your inbox.
Great! Check your inbox and click the link to confirm your subscription.
Error! Please enter a valid email address!