GET and POST requests in Python:

In this article, we will discuss about two HTTP (Hypertext Transfer Protocol) request methods, GET and POST requests and their implementation in Python for Data Science.

HTTP: It is a set of protocols which are designed to enable communication between clients and servers. Its working is a request-response protocol between a client and the server.

The client may be the web browser, and an application on computer hosting the website is the server.

In order to request a response from the server, there are two methods:

  • GET: This is there to request data from the server.
  • POST: This is there to submit data which is to be process to the server.

To make things clear, we let us have a look at a diagram.

GET and POST requests in Python for Data Science - PST Analytics

To make HTTP requests in Python, several HTTP libraries are there:

  • httplib
  • urllib
  • requests

From the above libraries, requests is the most simple and elegant one. To use Requests library, we use the following command:

pip install requests

Making GET request:

GET and POST requests in Python for Data Science - PST Analytics


In the above example, we have found the latitude, longitude, and format address of a certain location by sending GET request to Google Maps API. So, API gives us access to internal features of a program in a limit way. The data which is there is mostly in JSON format.

Important Point:

The URL which is there for GET request carries some parameters with it. In case of request library, parameters are define as dictionary. So, the parameters are later parse down, and then it add to the base url or api-endpoint.

We will print r.url after response object is create. The output will be:

The above is actual URL on which GET request is made.

In this, there is creation of response object ‘r’ which stores the request-response. The requests.get() method is there as we are sending a GET request. So, the two arguments which are pass are the url and the parameters dictionary.

  • data = r.json()

To retrieve the data from response object, we convert the raw response content in a JSON type data structure. In order to achieve this we use the json() method. In the end the information is extracted by parsing down the JSON type object.

Making POST request:

The above example shows how to paste source_code to by sending a POST request to PASTEBIN API.

Important features of the code:

GET and POST requests in Python for Data Science - PST Analytics

  • In this, we need to pass some data to API server. The data is there as a dictionary.

  • In this, a response object ‘r’ is there that stores the request-response. The method is there as we are sending a POST request. The two arguments pass are the url and the data dictionary.
  • In the response, the server will process the data sent to it and sends the pastebin URL of the source_code that can be access by using text.

The method is there for other tasks like filling and submitting the web forms, creating posts on FB timeline, etc.

Some Important points:
  • Whenever we use GET method, all the form data is encoded into URL; this is appended to action url as query string parameters. When we use POST, the form data will appear within the message body of HTTP request.
  • In case of GET method, the parameter data is limited to the things we can stuff into the request line (URL). It is safe to use less than 2K of parameters, but some servers can handle up to 64K of parameters. So, there is no such problem in POST method as data is sent in message body of HTTP request and not the URL.
  • GET method allows only sends data which consists of ASCII characters. POST method has no such problem.
  • POST is more secured than GET as data sent is part of URL. So, it is advisable to not use GET method in order to send passwords or some sensitive information.

To learn more about get and post method in python for data science, you can check this and this as well.


Leave a Reply

Your email address will not be published. Required fields are marked *

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