Intro to the Python Requests Module

11.28.2020

Why Use Requests

The requests module abstracts away many of the boilerplate code you would need to add if using the urllib standard library. Making HTTP (and some other protocol) requests are common to many apps, thus it is nice to have a library to assist. You can find a full list of request features here: https://requests.readthedocs.io/en/master/.

Getting Started

First, install the requests module:

pip install requests

HTTP requests are the most common type of request, and that is what we will show in the tutorial.

The following is how we make a request with the requests module.

import requests as req

resp = req.request(method='GET', url="http://www.webcode.me")
print(resp.text)

If you are familiar with REST (if not, check out a tutorial on that first), you will notice we can pass a string for the method. Request supports the all HTTP verbs: GET, PUT, POST, etc.

The requests module doesn't force us to remember the spelling for the method parameter; it provides helper methods such as get() and post().

import requests as req

res = req.get("https://cat-fact.herokuapp.com/facts")
print(res.text)

The requests module also supports helper functions for the common HTTP verbs. For example, here is a POST request.

payload = {'key1': 'value1', 'key2': 'value2'}

r = requests.post("https://cat-fact.herokuapp.com/facts", data=payload)
print(r.text)

As I mentioned before, the requests module helps remove lots of boiler plate. For example, the requests module will parse json responses for us.

import requests

res = requests.get('https://cat-fact.herokuapp.com/facts')
res.json()

Where to go next

That's all for this quick intro. The request module has many great features and their documenation is suberb. I'd suggest heading there next to check out all the examples: https://requests.readthedocs.io/en/master/