Python: HTTP GET requests

The objective of this post is to explain how to do HTTP GET requests using Python and the Requests library.


The objective of this post is to explain how to do HTTP GET requests using Python and the Requests library.

The simplest way to install this library is by using pip, as can be seen in the installation page of the library.

As usual with Python libraries, this one makes available a lot of functionalities that allow to perform HTTP requests in a very easy manner.

You can check the API description for this library here.


GET request

Doing a GET request with this library is very simple and can be done with very few lines of code. In this case, we will do a GET request on the jsonplaceholder website, which has a fake online REST API for testing and prototyping [1].

We are going to access a endpoint which will return a list of users in JSON format. The link is shown bellow and we can access it from a web browser to check the expected result.

For the Python code, we first need to import the Requests library. To do so, we just use the code shown bellow.

import requests

Now, we simply call the get method defined in this module and pass the URL as argument. We will store the result in a variable.

response = requests.get(‘′)

The returning value of this invocation will be a Response object, which has some methods available, as can be seen here.

To print the response text of our request we just call the text property on the Response object, as shown bellow. The code for this property can be seen here.

print response.text

So, with just 3 lines of code, we were able to perform a simple HTTP GET request and print the response.

Figure 1 shows the expected output when running the code on IDLE. We can see the JSON that is returned in the response of our GET request, which should be the same that we saw when accessing the URL via web browser.


Figure 1 – Output of the GET request with the Python Requests library.

In alternative to calling the get method, we could also have called a more generic method called request. In this case, in addition to passing the URL where we want to make the HTTP request, we also pass the information about which method we want to use (in this case, the GET method). The code using this alternative method is shown bellow.

response = requests.request(‘GET’, ‘′)
print response.text

We should receive the same response as before in IDLE’s command line.


Important: In order for WordPress to not escape the single quote in the end of URLs, I used a special character single quote. When copying and running the code, we need to change the single quote at the end of the URL to a regular one (just delete this and put a regular one). Otherwise, an error will occur and the code will not execute.




4 Replies to “Python: HTTP GET requests”

  1. Pingback: techtutorialsx
  2. Pingback: techtutorialsx

Leave a Reply