GET and POST requests using Python



What is HTTP?
HTTP — it is a set of protocols designed to enable communication between clients and servers. It works like a request-response protocol between client and server. 
The web browser can be the client, and the application on the computer that hosts the website can be the server.

So, to request a response from the server, there are basically two methods:

  1. GET : request data from the server.
  2. POST : send data to the server for processing.
  3. Here is a simple diagram that explains the basic concept of the GET and POST methods. 
    Now, to make HTTP requests in python, we can use several HTTP libraries like:

    The most elegant and simple of the above libraries — these are Requests. In this article, we will be using the requests library. To download and install the requests library, use the following command:

     pip install requests 

    OR, download it from here and install manually.

    Create a receive request

    # import request library

    import requests

     
    # api-endpoint

    URL = " http://maps.googleapis.com/maps/api/geocode/json "

      
    # place indicated here

    location = "delhi technological university"

     
    # defining the params parameter for parameters sent to the API

    PARAMS = { `address` : location}

     
    # send a request to receive and store the response as a response object

    r = requests.get (url = URL, params = PARAMS)

      
    # fetch json data

    data = r.json ()

     

      
    # extract latitude, longitude and formatted address
    # of the first matching location

    latitude = data [ `results` ] [ 0 ] [ `geometry` ] [ `location` ] [ ` lat` ]

    longitude = data [ `results` ] [ 0 ] [  `geometry` ] [ ` location` ] [ ` lng` ]

    formatted_address = data [ `results` ] [ 0 ] [ `formatted_address` ]

     
    # print output

    print ( "Latitude: % s Longitude:% s Formatted Address:% s "

      % (latitude, longitude, formatted_address))

    Output:

    The above example finds the latitude, longitude and formatted address of a given location by sending a GET request to the Google Maps API. API (Application Programming Interface) allows you to get limited access to the internal functions of the program. And in most cases, the data provided is in the format JSON (JavaScript Object Notation) (which is implemented as dictionary objects in Python!) … 

    Important points to draw a conclusion:

    •  PARAMS = {`address`: location} 

      The URL for a GET request usually contains some parameters. For a query library, parameters can be defined as a dictionary. These parameters are later parsed and added to the base URL or API endpoint. 
      To understand the role of parameters, try typing r.url after creating the response object. You will see something like this:

       http://maps.googleapis.com/maps/api/geocode/json?address=delhi+technological+university 

      This is the actual URL where the GET request was made

    •  r = requests.get (url = URL, params = PARAMS) 

      Here we create a response object & # 39; r & # 39;, which will store the request-response. We are using the questions.get () method because we are sending a GET request. We pass two arguments: url and a parameter dictionary.

    •  data = r.json () 

      Now, to extract data from the response object, we need to transform the raw content of the response into a JSON data structure. This is achieved using the json () method. Finally, we extract the information we need by parsing a JSON object.

    Making a POST request

    # import request library

    import requests

     
    # API endpoint definition

    API_ENDPOINT = " http://pastebin.com/api/api_post.php "

     
    # Your API key here

    API_KEY = "XXXXXXXXXXXXXXXXX"

      
    # your source code here

    source_code = "" "

    print (" Hello world! ")
    a = 1
    b = 2
    print (a + b)
    " ""

     
    # data to send to the API

    data = { ` api_dev_key` : API_KEY,

    `api_option` : `paste` ,

      `api_paste_code` : source_ code,

    `api_paste_format` : `python` }

      
    # send a mail request and save the response as a response object

    r = requests.post (url = API_ENDPOINT, data = data)

     
    # extract the response text

    pastebin_url = r.text

    print ( "The pastebin URL is:% s" % pastebin_url)

    This example shows how to paste source_code at pastebin.com by submitting a POST request to the Pastebin API. 
    First of all, you need to generate an API key by by registering here and then access your API key here.

    Important features of this code:

    •  data = {`api_dev_key`: API_KEY,` api_option`: `paste`,` api_paste_code`: source_code, `api_paste_format`:` python`} 

      Here again we need to transfer some data to the server API. We store this data as a dictionary.

    •  r = requests.post (url = API_ENDPOINT, data = data) 

      Here we create a response object & # 39; r & # 39;, which will store the request-response. We are using the questions.post () method since we are sending a POST request. We pass two arguments: url and a data dictionary.

    •  pastebin_url = r.text 

      In response, the server processes the data sent to it and sends the pastebin URL to your source_code, which may simply be accessed r.text .

    The requests.post method can be used for many other tasks, as well as filling and submitting the web — forms, posting timelines to your FB using the Facebook Graph API, etc.

    Here are some important points to consider:

    • When GET, all form data is URL encoded, appended to the action in URL as query string parameters. With POST, the form data appears in the body of the message of the HTTP request.
    • In the GET method, the parameter data is limited to what we can insert into the request string (URL). It is safest to use less than 2KB of parameters, some servers handle up to 64KB. There is no such problem in the POST method, because we send data in the body of the message of the HTTP request, and not in the URL.
    • Only ASCII characters are allowed for data transfer in the GET method. There is no such restriction in the POST method.
    • GET is less secure than POST because the submitted data is part of the URL. As such, the GET method should not be used when submitting passwords or other sensitive information.

    This blog is provided by Nikhil Kumar . If you like Python.Engineering and would like to contribute, you can also write an article using contrib.python.engineering, or email your article to [email protected] See my article appearing on the Python.Engineering homepage and help other geeks.

    Please post comments if you find anything wrong or if you would like to share more information on the topic discussed above.