Requests is a Python module that you can use to send all kinds of HTTP requests. It is an easy-to-use library with a lot of features ranging from passing parameters in URLs to sending custom headers and SSL Verification. In this tutorial, you will learn how to use this library to send simple HTTP requests in Python.
You can use Requests with Python version 2.6–2.7 and 3.3–3.6. Before proceeding further, you should know that Requests is an external library, so you will have to install it first before trying out the examples in this tutorial. You can install it by running the following command in the terminal:
Once you have installed the module, you can verify if it has been successfully installed by importing it using this command:
If the installation has been successful, you won’t see any error messages.
Making a GET Request
HTTP request is a packet of information send to establish a connection between user and the server
It is very easy to send an HTTP request using Requests. You begin by importing the module and then make the request. Here is an example:
All the information about our request is now stored in a Response object called
encoding/character map stores how each char is converted into bytes.Default encoding is UTF-8
You can also get the status code of the request using the
we can access the status code by :
You can access the cookies that the server sent back using
This means that
You can check if the response is a well-formed HTTP redirect that could have been processed automatically using the
The URL that you initially passed to the
Getting all this information about the webpage you are accessing is nice, but you most probably want to access the actual content.
If the content you are accessing is text pr the source code (html) of the webpage, you can use the
In the case of non-text responses, you can access them in binary form using
You can also get the raw response from the server using
Some files that you download from the internet using the Requests module may have a huge size. In such cases, it will not be wise to load the whole response or file in the memory at once. You can download a file in pieces or chunks using the
This method iterates over the response data in