Python: HTTP GET requests

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


Introduction

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.

http://jsonplaceholder.typicode.com/users

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(‘http://jsonplaceholder.typicode.com/users′)

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.

python-http-get-request

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’, ‘http://jsonplaceholder.typicode.com/users′)
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.

 

References

[1] http://jsonplaceholder.typicode.com/

4 thoughts on “Python: HTTP GET requests”

  1. Pingback: ESP8266 Webserver: Receiving GET requests from Python | techtutorialsx

  2. Pingback: ESP8266 Webserver: Receiving GET requests from Python | techtutorialsx

  3. Pingback: techtutorialsx

  4. Pingback: techtutorialsx

  5. Pingback: Python: Parsing JSON | techtutorialsx

  6. Pingback: Python: Parsing JSON | techtutorialsx

  7. Pingback: LinkIt Smart Duo: Installing and testing the Requests library for Python | techtutorialsx

  8. Pingback: LinkIt Smart Duo: Installing and testing the Requests library for Python | techtutorialsx

Leave a Reply