Python requests module - Learn how to use the requests module in Python?
The requests module in Python is one of the most used modules in Python and it is used to connect to the http/https server to perform various activities. In this section we are going to provide you many examples to show us the usages of requests module in Python.
What is the requests module in Python?
Python requests is one of most popular modules in Python having more than 30M downloads / week. GitHub reports over 30M downloads per week and it is used by a large number of Python developers around the world to develop Python programs that interact with the http/https servers. Due to the popularity of the library you can think of its credibility among the Python developers. You can simply install this library in your Python environment and start using it.
This module is developed to allow HTTP/1.1 requests to the http/https servers easily. You can simply create the object of the Python requests library and start using it to interact with the HTTP/1.1 compliant servers.
What are the methods of the Python requests library?
Now we will see the methods of this library and try to understand how it can be used in the interaction with the http/https complaint servers.
Here are the methods of requests library:
|Sr. No.||Method Name||Description|
|1.||delete(url, args)||The delete() method is used to the DELETE request to http server specified in the specified url.|
|2.||get(url, params, args)||The get() method of the requests API is used to send a GET request to the http/https server specified in the url. With this api you can send a request to the http/https server and get the result returned by the server.|
|3.||head(url, args)||The head() method of the is used to send a HEAD request to the server.|
|4.||patch(url, data, args)||If you want to send the patch request to the http server then use can use patch() method.|
|5.||post(url, data, json, args)||The post() method is used to send a POST request to the server specified in the url parameter.|
|6.||put(url, data, args)||The put() method is used to send a PUT request to the http server.|
|7.||request(method, url, args)||This method can be used to send a request of the specified method to the server.|
How to install requests library in your Python installation?
First of all you have to install requests library in your Python environment. You can check it by opening the Python terminal and then run following import:
>>> import requests
If this module is not installed then it will throw following error:
ModuleNotFoundError: No module named 'requests"
In this case you have to install the requests module into your Python environment. You can run following command in the terminal to install requests module of Python:
pip install requests
Above command will install requests library in your Python installation. After installing this library you will be able to use in your Python code.
How to use the Python requests module?
To use the Python requests module you have to first import this library in your Python code. Here is the example to import requests module in your Python code:
Above code will import the requests library in your Python program.
Above is the screen shot of the program in the Python IDE.
After importing you can use this library to work with the http/https server.
Examples of using python requests module: