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 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
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.
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, 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.
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.
requests.post('url') requests.put('url') requests.delete('url') requests.head('url') requests.options('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.
#app.py import requests res = requests.get('https://api.github.com/users/KrunalLathiya') data = res.json() print(data['following'])
#app.py python3 app.py 13 (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.
#app.py import requests res = requests.get('https://api.github.com/users/KrunalLathiya') print(res)
python3 app.py <Response >
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)
(pythonenv) ➜ pyt python3 app.py 200
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.