Making a Get request

The above example finds the 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 you to access the internal features of a program in a limited fashion. And in most cases, the data provided is in JSON(JavaScript Object Notation) format (which is implemented as dictionary objects in Python!).

Python3




# importing the requests library
import requests
 
# api-endpoint
URL = "http://maps.googleapis.com/maps/api/geocode/json"
 
# location given here
location = "delhi technological university"
 
# defining a params dict for the parameters to be sent to the API
PARAMS = {'address':location}
 
# sending get request and saving the response as response object
r = requests.get(url = URL, params = PARAMS)
 
# extracting data in json format
data = r.json()
 
 
# extracting 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']
 
# printing the output
print("Latitude:%s\nLongitude:%s\nFormatted Address:%s"
    %(latitude, longitude,formatted_address))


Output:

 

Important points to infer:

PARAMS = {'address':location}

The URL for a GET request generally carries some parameters with it. For the requests library, parameters can be defined as a dictionary. These parameters are later parsed down and added to the base URL or the API endpoint. To understand the role of the parameter, try to print r.url after the response object is created. You will see something like this:

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

This is the actual URL on which the GET request is made

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

Here we create a response object ‘r’ which will store the request-response. We use requests.get() method since we are sending a GET request. The two arguments we pass are URL and the parameters dictionary.

data = r.json()

Now, in order to retrieve the data from the response object, we need to convert the raw response content into a JSON-type data structure. This is achieved by using json() method. Finally, we extract the required information by parsing down the JSON-type object.

GET and POST Requests Using Python

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

Similar Reads

What is HTTP?

HTTP is a set of protocols designed to enable communication between clients and servers. It works as a request-response protocol between a client and a server. A web browser may be the client, and an application on a computer that hosts a website may be the server. So, to request a response from the server, there are mainly two methods:...

Making a Get request

The above example finds the 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 you to access the internal features of a program in a limited fashion. And in most cases, the data provided is in JSON(JavaScript Object Notation) format (which is implemented as dictionary objects in Python!)....

Making a POST request

...

Contact Us