Course Content

Requests Module For HTTP Requests | Python Tutorials For Absolute Beginners In Hindi #81

Let's just open our PyCharm and create a new file.

 

 

 

 

We are going to name our file req.py here.

In today's tutorial, we will learn about the request module and how we can send HTTP requests through our program directly. By sending  HTTP requests, we get a response object as a return. The request library in Python is the standard for making HTTP requests. Let us first understand what does HTTP means.

What is HTTP?

HTTP stands for the 'Hypertext Transfer Protocol,'. It is a set of protocols that are designed to enable communication between clients and servers. Between clients and servers, it works as a request-response protocol. To request a response from the server, we can request data from the server, or we can submit data to be processed to the server.

What Is Requests Module?

The response data depends on our type of request. The request module is not a built-in Python module; instead, we have to download it manually. Requests module is used to send all kinds of HTTP requests. It is also one of the most downloaded modules in Python because all the web related software and programs must have it in them.

Install Python Requests:-

To work with the requests module, the first step is to install the module in Python. To download a request module, we can run the code:

 pip install requests

After that, we have to import our module into the program, the same as we import all other modules.

import requests

 Built-in methods in the request module:-

There are a lot of built-in methods in request module, such as delete(), get(), Head(), put(), etc. We will see the working of the get() function in this tutorial in detail. 

get():

As from the name, we can detect that the get function returns us with some information about the site we requested. All the information is stored in the object we used to send the request. We can access different kinds of information through it, such as status, header, cookies, etc. To make a GET request, invoke

The basic syntax is:

requests.get(URL, params={key: value}, args)

URL: this is the URL of the website where we want to send the request. 

Params: this is a dictionary or a list of tuples used to send a query string. 

Args: It is optional. It can be any named arguments offered by the get method.

We can also fetch all the data from the homepage of a website into an HTML format using the request module. Few important types of methods defined in the request module are as follows:

Methods

Working

put( ):

 

 It is used to send a put request to a variable. It is usually used to update or completely change the resources of a specific URL.

 

delete( ):

 

Delete is used to delete the specific resource, specified by URL.

 

head( ):

 

The head method returns a header for a specific resource

post( ):

 

Post requests take with it the data to the server to update it with.

patch( ):

 

The patch is used to send patch requests. It is used to apply partial modifications to a resource. It carries with it the instructions for the modification.

For more detail, you can check the Python documentation about the requests module.

 

Code as described/written in the video


import requests
r = requests.get("https://financialmodelingprep.com/api/company/price/AAPL")
print(r.text)
print(r.status_code)

# url = "www.something.com"
# data = {
#     "p1":4,
#     "p2":8
# }
# r2 = requests.post(url=url, data=data)

Comments(2)

Zeeshan1345 1 month, 3 weeks ago
AOA
bhai jab main ne "import requests" apply kia tu Pycharm ne ye reply dia.
"Traceback (most recent call last):
  File "C:/Users/Qurrat/Desktop/payton new begining/Module Python/Requestes Module.py", line 3, in <module>
    import requests
ModuleNotFoundError: No module named 'requests'
"
Zeeshan1345 1 month, 3 weeks ago
please guide kr dain k ye request module k se instal kro pycharm main

Resources

No downloadable resources for this video. If you think you need anything, please post it in the QnA!

Course Announcements

Any Course related announcements will be posted here