Python

Python Requests Get Query Parameters

Python’s Requests library includes various functions that the user can utilize to make different types of HTTP requests to a specified URL with specific parameters. One of the methods in this library is the “get()” method which is used to make a GET request to a specific destination and return the response of the server to the calling variable.

Let’s have a better understanding of the working of the get() method by going over its syntax:

Syntax of the get() Method

The get() method’s syntax is as follows:

resultVar = requests.get( targetURL , paramQuery , args)

In this syntax:

  • resultVar is the variable that will store the result/response of the server
  • targetURL is the address of the server that will be pinged with a GET request.
  • paramQuery is the query to be passed to the server in the form of key-value tuples
  • args are the additional arguments that can be used with the get() method

To use the get() method, you need to first install the “requests” library into your environment by using the following command:

pip install requests

Once you have installed the library, you can move on to using the get() method in the examples given below.

Example 1: Making a Simple Get Request to a Server Using the get() Method
Let’s try making a simple request to Google and print out the response code from Google’s server using the following code:

import requests

resVar = requests.get("https://www.google.com")

print(resVar)

When this code is executed, it will produce the following output on the terminal:

As you can see, the response code is “200” which means that the server has successfully received the request and returns the query in the message body.

Example 2: Using the json() Method to Show the Response With get() Method
If you want to show the json response provided by the user, the user can use the json() method on the resulting variable of the get() method. However, the user has to make a request to a server that is going to return the response in the form of a json string in the message body.

To demonstrate this, let’s make a request to the Reqres API using the following code and print the result of the json() method:

import requests

resVar = requests.get("https://reqres.in/api/users")

print(resVar.json())

When this code is executed, it will produce the following results on the terminal:

The output displays the json response of the Reqres API.

Example 3: Using Specific Query in the get() Method
To pass a specific Query in the get() method, place the query in a tuple and pass it in the second argument of the get() method. Let’s make a GET request to the Reqres API to show the content of the 2nd page using the following code snippet:

import requests

resVar = requests.get( "https://reqres.in/api/users", params={'page': '2'})

print(resVar.content)

When this code is executed, it will produce the following result on the terminal of your machine:

The output confirms that you were able to pass a specific Query in your get() method as well.

Conclusion

The user can make use of the get() method from the requests library to make a GET request to a specific destination URL/server along with specific queries. However, to use this get() method, you need to first install the requests library to your machine using the command “pip install requests.” To display the response of the server in the form of json, the user can apply the json() method on the resulting variable of the get() method.

About the author

Abdul Mannan

I am curious about technology and writing and exploring it is my passion. I am interested in learning new skills and improving my knowledge and I hold a bachelor's degree in computer science.