Requests Library for HTTP

Janhavi Dahihande
Sep 5, 2018 · 3 min read

What is a Library?

Library is a collection of pre-compiled routines that can be used by a program. These routines are also called Modules and stored in Object format. Linking a module explicitly every time you need it in your code can be a bit of a task and highly non-recommended. Libraries come to your rescue by loading the module for you and still create a standalone application.

What is Requests library?

As quoted on their official website, “Requests is an elegant and simple HTTP library for Python, built for human beings.” What does it do exactly?

Requests allows you to send HTTP/1.1 requests, without the need for manual labor. There’s no need to manually add query strings to your URLs, or to form-encode your POST data.

Idea behind Request?

  • Requests is an Apache2 Licensed HTTP library, written in Python
  • It is designed to be used by humans to interact with the language
  • It will allow you to send HTTP/1.1 requests using Python
  • Latest version is 2.19.1 (2018–06–14)

Urllib versus Requests

To quote the current Python 3 urllib package documentation: “urllib is a package that collects several modules for working with URLs”. In other words, urllib handles generic URL processing. As opposed to that, Requests concentrates on the focused HTTP + JSON processing.

As a result, for developers looking for HTTPS client processing, Requests seems to be the go-to library which is simple, quick and easy. For more information, head over to this article by Nick Coghlan.

Installation of Requests

Installing the Requests library is super-easy. And there are a lot of ways to do it. To explore the various options, visit the installation documentation.

I have used pip to install the library.

Go to the python interpreter and type the following command:

pip install requests

And that’s it. You have successfully installed the library!

Using in your code:

The use of the library in you code is as simple as it gets. Just add the import statement at the beginning of your code:

import requests

Request code:

Making a request means to get any information from a destination. Requests library has been formulated to do just the same. For requesting a webpage, you can use the following code:

Response code:

The destination(or the server) returns a response and the same can be used according to our use-case.

For example:

There are a lot many methods apart from text, for example, status_code, url, etc. Check out the official documentation to try out more methods.

How does it work?

Whenever we call requests.get, a PreparedRequest object is created and sent to the server for getting the required information. Secondly, a Response object is generated when Requests gets some response back from the server. What exactly is present in the Response object?

Response object = (Information from server) + Original Request object

As a result, we can access the information sent by the servers and also the information in the original request sent by us.

For instance, in the previous example, if we want to access the headers for the response we received, we can do the following:

Also, if we want to access the headers of the request we sent to the server, we can do it easily.

Requests methods

All of Requests functionality can be accessed by 7 methods which return an instance of the Response object.

  1. requests.request(method, url, **kwargs): Constructs and sends a Request
  2. requests.head(url, **kwargs): Sends a HEAD request
  3. requests.get(url, params=None, **kwargs): Sends a GET request
  4. requests.post(url, data=None, json=None, **kwargs): Sends a POST request
  5. requests.put(url, data=None, **kwargs): Sends a PUT request
  6. requests.patch(url, data=None, **kwargs): Sends a PATCH request
  7. requests.delete(url, **kwargs): Sends a DELETE request

In Use?

Requests is one of the most downloaded Python packages of all time, pulling in over 400,000 downloads each day. The big players in the industry like Nike, Lyft, Amazon, Google, Spotify, etc use Requests internally.

So go on! Use Requests in your project and feel the amazing difference it makes in simplifying your development process.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade