Latest Code Tutorials

Python Requests: How to Send Network Request in Python

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.

What is HTTP?

An HTTP is a set of protocols designed to enable 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 website may be the server.

Python Requests

Python requests 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.

How to Install Requests module

You can make use of pip, easy_install, or tarball package managers. For example, I am using PIP to install the requests library. Unfortunately, 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 file.

import requests

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


import requests

res = requests.get('')
data = res.json()

In the above code, first, we have imported the requests module and then use the GET method to send the 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. 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 response’s encoding 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 requests is the following.'url')

Python GET Requests

HTTP methods such as GET and POST resolve which action you’re trying to perform when making the HTTP request. Besides GET and POST, there are several other common methods you will use later in this example.

One of the most common HTTP methods is GET. The GET request indicates that you’re trying to get or retrieve data from the specified resource. To make the GET request, invoke requests.get().

See the following code.

import requests

res = requests.get('')
data = res.json()


(pythonenv) ➜  pyt

I am following 13 people on my Github profile.

Python GET Requests Response

A Response is a robust object for inspecting the results of a request. Let’s make that same request again, but this time store the return value in the variable so that you can get a closer look at its attributes and behaviors.

import requests

res = requests.get('')


<Response [200]>

In this example, you’ve got the return value of get(), an instance of Response, and stored it in a variable called the Response.

You can now use the Response to see a lot of information about the results of your GET request.

We can get the status code using the following code.

import requests

res = requests.get('')



(pythonenv) ➜  pyt python3

The first bit of information that you can gather from Response is the status code. A status code informs you of the status of the request.

For example, a 200 OK status means that your request was successful, whereas a 404 NOT FOUND status means that the resource you were looking for was not found. In addition, there are many other possible status codes to give you specific insights into what happened with your request.

That’s it for this tutorial.

1 Comment
  1. Romen says

    Thanks for sharing Python Requests Tutorial With Example related full information with us.

Leave A Reply

Your email address will not be published.

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