Python How to Read Response After Posting

This post discusses two HTTP (Hypertext Transfer Protocol) request methods  GET and Mail requests in Python and their implementation in python.

What is HTTP?
HTTP is a set of protocols designed to enable communication between clients and servers. It works as a asking-response protocol between a client and server.
A web browser may be the customer, and an awarding on a reckoner that hosts a website may exist the server.

And so, to request a response from the server, at that place are mainly two methods:

  1. GET : to asking data from the server.
  2. POST : to submit information to be processed to the server.

Here is a simple diagram which explains the basic concept of Become and POST methods.iservice_post_get
At present, to brand HTTP requests in python, we can use several HTTP libraries like:

  • httplib
  • urllib
  • requests

The nigh elegant and simplest of in a higher place listed libraries is Requests. We will be using requests library in this article. To download and install Requests library, employ following command:

pip install requests

OR, download it from here and install manually.

Making a Go asking

import requests

location = "delhi technological university"

PARAMS = { 'address' :location}

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

data = r.json()

breadth = data[ 'results' ][ 0 ][ 'geometry' ][ 'location' ][ 'lat' ]

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

formatted_address = information[ 'results' ][ 0 ][ 'formatted_address' ]

print ( "Latitude:%s\nLongitude:%due south\nFormatted Address:%due south"

% (latitude, longitude,formatted_address))

Output:
GET-and-POST-requests-using-Python

The in a higher place example finds latitude, longitude, and formatted address of a given location by sending a GET request to the Google Maps API. An API (Application Programming Interface) enables y'all to admission the internal features of a program in a limited fashion. And in about cases, the data provided is in JSON(JavaScript Object Annotation) format (which is implemented as dictionary objects in Python!).

Of import points to infer :

  • PARAMS = {'address':location}

    The URL for a GET request generally carries some parameters with it. For requests library, parameters tin can exist defined as a dictionary. These parameters are later parsed downwards and added to the base url or the api-endpoint.
    To understand the parameters function, endeavor to print r.url after the response object is created. You volition see something like this:

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

    This is the actual URL on which Get request is made

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

    Here nosotros create a response object 'r' which will store the asking-response. Nosotros use requests.get() method since nosotros are sending a Go asking. The two arguments we pass are url and the parameters lexicon.

  • data = r.json()

    At present, in order to recollect the data from the response object, we need to convert the raw response content into a JSON type data structure. This is accomplished past using json() method. Finally, we extract the required data past parsing downward the JSON type object.

Making a Post request

import requests

API_KEY = "XXXXXXXXXXXXXXXXX"

source_code =

data = { 'api_dev_key' :API_KEY,

'api_option' : 'paste' ,

'api_paste_code' :source_code,

'api_paste_format' : 'python' }

r = requests.postal service(url = API_ENDPOINT, data = data)

pastebin_url = r.text

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

This example explains how to paste your source_code to pastebin.com by sending Mail service request to the PASTEBIN API.
First of all, yous volition need to generate an API key by signing up here and and so access your API central here.

Important features of this lawmaking:

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

    Here again, we will demand to pass some data to API server. We store this information as a lexicon.

  • r = requests.mail service(url = API_ENDPOINT, data = data)

    Hither we create a response object 'r' which will store the request-response. Nosotros apply requests.post() method since nosotros are sending a Mail service asking. The two arguments we pass are url and the information lexicon.

  • pastebin_url = r.text

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

requests.post method could be used for many other tasks every bit well like filling and submitting the web forms, posting on your FB timeline using the Facebook Graph API, etc.

Here are some important points to ponder upon:

  • When the method is Become, all class data is encoded into the URL, appended to the activity URL as query cord parameters. With Mail service, form data appears within the bulletin torso of the HTTP request.
  • In GET method, the parameter data is limited to what we tin can stuff into the request line (URL). Safest to utilise less than 2K of parameters, some servers handle upwardly to 64K.No such problem in POST method since we send data in bulletin body of the HTTP request, not the URL.
  • Only ASCII characters are allowed for data to be sent in GET method.There is no such restriction in POST method.
  • GET is less secure compared to Postal service because data sent is part of the URL. So, GET method should non be used when sending passwords or other sensitive information.

This weblog is contributed by Nikhil Kumar. If you like GeeksforGeeks and would similar to contribute, yous can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your commodity actualization on the GeeksforGeeks primary folio and help other Geeks.

Please write comments if you detect anything incorrect, or y'all desire to share more information about the topic discussed above.


Python How to Read Response After Posting

Source: https://www.geeksforgeeks.org/get-post-requests-using-python/

0 Response to "Python How to Read Response After Posting"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel