Query String With Params for Requests in Python

Query String With Params for Requests in Python

We can create HTTP requests in Python using the requests library. This library is very simple and provides different functionalities to create requests and inspect the retrieved data.

GET and POST requests are the generally used HTTP requests. The requests library provides the get() and post() functions to create such HTTP requests.

This tutorial will demonstrate the use of the query string parameters while creating a request with the requests library in Python.

Query String for a Request Using params in Python

An HTTP request to retrieve or post data requires additional details. Query string parameters refer to such values in addition to the URL.

The GET requests are used to fetch some data.

For creating GET requests, we provide the params parameter in the get() function. The general convention uses the params parameter to provide query string parameters in the GET request.

For POST requests, we follow the data parameter, although we can use both since it’s just a general standard.

Let’s now see an example of creating a GET request using the requests library and providing the query string parameters.

import requests

url = 'http://www.google.com'
params = {'sessionKey': 'xyAbc5862', 'format': 'json'}
data = requests.get(url, params=params)

We retrieve some data in the above example. The query string parameters are provided using the params parameter in the get() function.

The final result returned is a requests.Response object. We can extract the text, status code, and other useful values using different attributes of this object.

Author: Manav Narula
Manav Narula avatar Manav Narula avatar

Manav is a IT Professional who has a lot of experience as a core developer in many live projects. He is an avid learner who enjoys learning new things and sharing his findings whenever possible.

LinkedIn

Related Article - Python Requests

  • Ignore SSL Security Certificate Check in Python Requests
  • Set User Agent Using Requests in Python
  • Make an API Call With Token in Python
  • Use Requests Module to Post Form Data in Python
  • Set Maximum Retries for Requests in Python