Python Requests: How to Send Network Request in Python

What is HTTP?

An HTTP is a set of protocols 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 written in the Python Programming Language. The Requests module allows you to send HTTP requests like GET or POST.

You can add the headers, form data, multipart files, and parameters with simple dictionaries and access the response data similarly.

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

You must import the appropriate module to work with the Requests library in Python.

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

OK, 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, we first imported the requests module and then used the GET method to send the GET request to the GitHub api, saved the Response inside the res variable, and then called the json() method on that object, and we get the dictionary.

Then I called the dictionary by its key, and we got the result.

Importing the Requests Module

My GitHub profile has 311 followers, 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.


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, you will use several other common methods 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.

#app.py

import requests

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

Output

#app.py

python3 app.py
13
(pythonenv) ➜  pyt

I am following 13 people on my Github profile.

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.

#app.py

import requests

res = requests.get('https://api.github.com/users/KrunalLathiya')
print(res)

Output

python3 app.py
<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.

#app.py

import requests

res = requests.get('https://api.github.com/users/KrunalLathiya')

print(res.status_code)

Output

(pythonenv) ➜  pyt python3 app.py
200

The first information you can gather from the 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.

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.

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

That’s it for this tutorial.

1 thought on “Python Requests: How to Send Network Request in Python”

Leave a Comment

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