API Navigator Logo

Python Requests with Query Parameters

Author: Newtum

This example demonstrates how to add URL query parameters to your GET requests using the Python Requests library.

Using the `params` Argument

The most convenient and safest way to add query parameters is to pass a dictionary to the `params` argument of `requests.get()`. Requests will automatically encode the parameters and append them to the URL.

import requests

url = 'https://jsonplaceholder.typicode.com/comments'

# Define the query parameters in a dictionary
my_params = {
    'postId': 1,
    '_limit': 2
}

response = requests.get(url, params=my_params)

print(f"Request URL: {response.url}")
print("\nResponse JSON:")
print(response.json())