python requests query string

Python Requests Query String

Query strings are used to pass additional information to the server through the URL. Python Requests module is a powerful library to send HTTP requests using python. It is widely used in web scraping, API testing, automation, and many more.

Pass Query String using URL Parameter

We can pass query strings using URL parameters. Let’s take an example of fetching weather data from the Open Weather API by passing city name as a query string parameter:

import requests

city_name = 'London'
response = requests.get('{}&appid='.format(city_name))

In the above code, we passed the city name as a parameter in the URL. The server will extract the query string parameter and process the request accordingly.

Pass Query String using params keyword argument

We can also pass query string using the params keyword argument of the requests.get() method. Let’s take an example:

import requests

url = ''
params = {'postId': 1}
response = requests.get(url, params=params)


In the above code, we passed the postId as a parameter in the params keyword argument. The requests library will encode the parameters and append them to the URL.