Python allows you to connect a local host to an external server using HTTP (the hypertext transfer protocol) by hitting a particular URL.

The code which performs this type of operation makes use of libraries such as:

  1. httplib
  2. urlib
  3. requests

Of these, the ‘requests‘ library is the simplest and most commonly used.

There are two major functions in the requests library for receiving and sending information from and to the server: GET and POST respectively.


All ‘GET’ requests in Python take in two inputs:

  1. A URL string, (this is the location that you want to request).
  2. A dictionary of parameters, which specifies what additional key-values you are going to send along with the request. Well, actually a dictionary of dictionaries. This is also shown in the diagram below:
Dictionaries can be represented with this image.
If a dictionary can be represented like this…
Parameters for GET requests in Python
… then this is the dictionary of parameters that we are creating (PARAMETERS_GET)

When you hit the URL and send the parameters, you will receive the results back in an object, which can be converted to json (JavaScript Object Notation) format and thereby be printed/manipulated.

Let’s take an example of the GET code. While making the request to the URL, you would want to provide additional parameters while hitting it.

You can send these parameters by creating a dictionary, in our case, ‘PARAMETERS_GET’. This has been capitalized so that you would notice how we’re creating this in the code.

The url that we are planning to hit is : .

Example for GET requests in Python:

import requests
    "data": {
        "id": "n",
        "email": "email",
        "first_name": "fname",
        "last_name": "lname",
        "avatar": "something"
r = requests.get(url = "", params = PARAMETER)
data = r.json()
{'data': {'id': 2, 'email': '[email protected]', 'first_name': 'Janet', 'last_name': 'Weaver', 'avatar': ''}}

You can verify the retrieved data (in other words, you can verify the output) by directly opening the url on your browser (because this URL sends the same data irrespective of the parameters we sent):

Additional points to note about the ‘GET’ requests in Python:

  1. When you submit a form on a website, all the form values are encoded into the URL passed in the ‘GET’ method and appended to a hyperlink generated. For example, a GET search looks like this and is directly visible to the user:
  2. You can use only a limited amount of parameter data – bounded by the server capacity. There are servers which handle up to 64 K parameters, but a safe limit on use would be 2 K.
  3. The data sent must be encoded in the ASCII format since URLs support only ASCII data.

It is inadvisable to receive passwords and other sensitive information using ‘GET’, as the sent data is actually just appended to the URL and thereby, not well encrypted.


Just like GET, the POST requests in Python also take two inputs:

  1. The URL where you need to pass the data.
  2. The JSON dictionary value which contains the parameters that you would send.
PARAMETERS for POST request in Python.
PARAMETERS_POST is a simple dictionary.

The following code posts the key-value data in the dictionary “PARAMETERS_POST” to the URL

To see the posted text, you store the result object and check its ‘text’ attribute.

import requests
PARAMETERS_POST = {"email":"[email protected]","password":"pistol"}
f ='' , json=PARAMETERS_POST)

Additional Points to note about ‘POST’ requests in Python:

  1. The form data is in the message body of the HTTP request, not the URL.
  2. As the data is sent in the message body, there aren’t any restrictions on how much data can be sent.
  3. Any type of character encoding is permitted when using ‘POST’.
  4. The POST method can be used to send sensitive information as it is more secure than ‘GET’.
Support the Python Engager course by sharing it with your followers!

Leave a Reply

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