AppDividend
Latest Code Tutorials

Python Requests Tutorial With Example | Requests Module

0 83

Get real time updates directly on you device, subscribe now.

Python Requests Tutorial With Example or Requests Module in Python is today’s topic. Requests is an Apache2 Licensed HTTP library which is written in the Python Programming Language. It is designed to interact with apis. That means you don’t have to manually add the query strings to URLs, or form-encode your POST data. The Requests module is an elegant and simple HTTP library for Python.

What is HTTP?

An HTTP is the set of protocols designed to enable the communication between clients and servers. It works as a request-response protocol between a client and the server. The web browser may be the client, and an application on a remote computer that hosts a web site may be the server.

Python Requests Tutorial With Example

Requests allow you to send HTTP requests like GET or POST. You can add the headers, form data, multipart files, and parameters with simple Python dictionaries, and access the response data in the same manner.

How to Install Requests

You can make use of pip, easy_install, or tarball package managers. I am using PIP to install the requests library. I am using Python3, so I have to use pip3 instead of pip.

pip3 install requests

 

Python Requests Tutorial With Example

Importing the Requests Module

If you want to work with the Requests library in Python, you must import the appropriate module. You can do this only by adding the following code at the beginning of your script. Write the following code inside the app.py file.

import requests

Okay, now let’s send a GET request to the My Github Profile.

# app.py

import requests

res = requests.get('https://api.github.com/users/KrunalLathiya')
data = res.json()
print(data['login'])

In the above code, first, we have imported the requests module and then use the GET method to send a GET request to the GitHub api and save the response inside the res variable and then call the json() method on that object and we get the dictionary and then I have called the dictionary by its key and we get the result.

 

Importing the Requests Module

My Github profile has 311 followers right now, so it has returned the 311. In the future, it might be different depending on when you are taking this tutorial.

When you make the request, Requests makes educated guesses about the encoding of the response based on the HTTP headers.

Other HTTP requests

Till now, we have used the GET request. You can also send the POST, PUT, DELETE, HEAD, and OPTIONS requests.

The syntax for all the request is following.

requests.post('url')
requests.put('url')
requests.delete('url')
requests.head('url')
requests.options('url')

Finally, Python Requests Tutorial With Example | Requests Module in Python is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.