Learn How to Use Python Requests – For Beginners

New to Python Requests? Well, you’ve reached the right place. This post is crafted with the help of python experts; we’ll help all the beginners understand the basics of Python Requests.

 

Learning of Blog

  • Introduction
  • What is HTTP?
  • Python Requests Module

 

Introduction

How do you think you can access the internet? Is it just a simple click of the mouse and a few keyboard taps? No! When you access the internet for some information – go to YouTube for watching funny videos, upload data on Google Drive, or access your favorite Facebook, there’s a process to it. Your system sends a request through your browser to the server of these websites. Once it is approved, you are granted access.

 

What is HTTP?

HTTP (Hypertext Transfer Protocol) is a set of protocols that facilitate the connection between a client and a server. In the example above, your web browser is the client, and the web site host is the server. While programming in Python, you can make these requests using a module named Requests.

 

Python Requests Module

As mentioned above, HTTP is a request-response method to facilitate access between a server and client. In most cases, your web browser is the client, and the system hosting the site is the server.

Handling HTTP requests is not as easy as drinking a glass of water. If we talk about Python in terms of managing HTTP related operations, it offers two built-in modules, urllib and urllib2. Both modules provide unique functionalities and are often used together. 

 

Although urllib2 is preferred more than urllib because urllib is a bit confusing, its documentation is not proper, and the developers need to write long codes for a simple HTTP request.

 

To simplify such processes, there’s easy to use a third-party library, called Requests. Most developers prefer to use it instead of inbuilt modules. It establishes the request-access relationship between the client and the server on the web. It is a handy library with plenty of essential methods and features to send HTTP requests.

 

There are two methods used for requesting a server for access:

Learn How to Use Python Requests - For Beginners

  • GET – This method requests data from a server.
  • POST – This method submits data to the server for it to process.

 

How to Install Requests

Requests module is easy to use and has a simple API beneficial for handling data requests. It includes some cool features, like sending custom headers, adding headers, passing parameters within URLs, and much more.

 

To start your work with the requests module, install the module in Python. How? Type in the following code in Python:

 

$ pip install requests

 

In case you want to use Pipenv, type in the following code:

 

$ pipenv install requests

 

After the installation is complete, you can use it within your programs by importing it. Use the following code:

import requests

 

Now, let us understand the most important methods of the python requests module – GET and POST.

GET Request

This method is used if you wish to obtain data from a resource on the web. The basic syntax is:

 

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

 

Here, url is the URL of the website from where you access the data and send the request. The params is a dictionary or a list of tuples used to send a query string. The args can be any one or more of the various named arguments (optional) offered by the GET method. And, these are:

allow_redirects – This is a Boolean value used to enable or disable redirection. Default value: True

auth – This is a tuple for enabling an HTTP authentication. Default value: None

cert – This can be a tuple or a string for mentioning a cert file or key. Default value: None

timeout – It is can be a tuple or a number that indicates the number of seconds to wait for the client to establish a connection or before sending a response. Default value: None

verify – This is a string or a Boolean value that indicates the server’s TLS certificate verification.

 

The default value is True.

 

cookies – This is a dictionary of cookies that you want to send to the specified URL. Default value: None

headers – This is a dictionary containing HTTP headers that you wish to send to a URL. Default value: None

stream – It is a Boolean value True or False that indicates whether the response should be streamed (True) or immediately downloaded (False). Default value: False

proxies – This is a dictionary of the protocol for the proxy URL. Default value: None

Learn More: Python Libraries for Machine Learning

 

After successfully sending the GET request, the method will return requests. Response object. This object stores the response that is obtained from the server. You can store the result of the get() method in a variable. Then, you can examine the details of this response. The important properties that help you in this regard are as follows:

 

response.content – This gives you the content of the data of the response.

response.status_code – This gives you the status of your request. For example, 200 OK means your request was successful, but 404 NOT FOUND means your request could not locate the resource for you.

response.cookies – This is used for obtaining a CookieJar object having all the cookies you got from the server.

POST Request

Post() method is used to send information to a server. The basic syntax for the request is:

 

requests.post(url, data={key: value}, json={key: value}, args)

 

Some of the important parameters are:

 

url – This is the URL where you want to send some data. This is a mandatory parameter.

data – This is an optional parameter that specifies a dictionary, file object or tuple you want to send to the URL.

json – This is the JSON object to be sent to the URL.

args can be any of the different named arguments, such as:

 

files – This is a dictionary of files for sending to the URL.

headers – A dictionary of HTTP headers to send to the specified URL.

cookies – This indicates the dictionary of cookies that you may want to send.

Just like get(), the post() method also returns a requests.Response object.

Conclusion

The post on python requests module will help you in getting a basic understanding of server requests. If you wish to learn more about Python and become a Python expert, check out our python crash course.