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.

GET

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 :  https://reqres.in/api/users/2 .

Example for GET requests in Python:

1
2
3
4
5
6
7
8
9
10
11
12
13
import requests
PARAMETERS_GET = {
    "data": {
        "id": "n",
        "email": "email",
        "first_name": "fname",
        "last_name": "lname",
        "avatar": "something"
    }
    }
r = requests.get(url = "https://reqres.in/api/users/2", params = PARAMETER)
data = r.json()
print(data)

1
2
Output:
{'data': {'id': 2, 'email': 'janet.weaver@reqres.in', 'first_name': 'Janet', 'last_name': 'Weaver', 'avatar': 'https://s3.amazonaws.com/uifaces/faces/twitter/josephstein/128.jpg'}}

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): https://reqres.in/api/users/2

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: https://www.google.com/search?q=test
  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.

POST

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  https://reqres.in/api/register.

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

1
2
3
4
import requests
PARAMETERS_POST = {"email":"eve.holt@reqres.in","password":"pistol"}
f = requests.post('https://reqres.in/api/register' , json=PARAMETERS_POST)
print(f.text)

1
2
Output:
{"id":4,"token":"QpwL5tke4Pnpja7X4"}

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’.

Leave a Reply

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