send get & post requests in python

How to Send GET & POST requests using python

Python is a powerful programming language that allows you to do loads of things. Sometimes you may need to send HTTP requests to external URLs from within your Python application or website, retrieve the response data and use it to perform further operations. In this article, we will look at how to send GET & POST requests in python.


How to Send GET & POST requests using python

Python provides various third-party libraries like httplib, urllib, and requests to help you send requests & retrieve responses.

For our purpose, we will use requests module which is easy to use. Here is the command to install it in your system.

pip install requests

Also read: How to Install Pip in Ubuntu Linux


Make GET Requests

Here is a simple python script to send GET request.

# importing the requests library
import requests
  
# api-endpoint
URL = "http://example.com/api-endpoint"
  
# location given here
location = "mumbai"
  
# 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()
  
  
print(data['key'])

Let us look at the above code in detail. First we import Requests module.

import requests

Then we set the URL that we need to send GET request to.

URL = "http://example.com/api-endpoint"

Next we set the URL parameter address that we need to send, as a python dictionary.

PARAMS = {'address':location}

Then we send the GET request using requests.get command. It returns response object.

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

We convert the response into JSON and save it in data variable.

data = r.json()

Finally we print one of the key values using print function.


Make POST Requests

Similarly, you can make POST requests using requests.post command. Here is a simple script to send POST request.

# importing the requests library
import requests
  
# api-endpoint
URL = "http://example.com/api-endpoint"
  
# location given here
location = "mumbai"
  
# defining a params dict for the parameters to be sent to the API
DATA = {'address':location}
  
# sending get request and saving the response as response object
r = requests.post(url = URL, data = DATA)
  
# extracting data in json format
response = r.json()
  
  
print(response['key'])

Let us look at the above code in detail. First we import Requests module.

import requests

Then we set the URL that we need to send POST request to.

URL = "http://example.com/api-endpoint"

Next we set the POST data that we need to send, as a python dictionary.

DATA = {'address':location}

Then we send the POST request using requests.post command. It returns response object.

r = requests.post(url = URL, data = DATA)

We convert the response into JSON and save it in data variable.

response = r.json()

Finally we print one of the key values using print function.

Please note that the python requests module can also be used to send other types of HTTP requests such as PUT, PATCH, HEAD, CUSTOM, not just GET & POST requests.

In this article, we have learnt how to send GET & POST requests using python. It is important to note that requests.get() function accepts param argument for request parameters while requests.post() function accepts data parameter for POST data.

Also, in both our examples, we convert response into JSON. Whether you convert response into JSON, Text or some other format, depends on the response data type and values. You will need to change it according to your requirement.

As you may already know, there is a limit on the amount of data that can be sent via GET requests but no such limitation exists for POST requests. Also, data sent in GET requests is insecure while that sent for POST request is secure. GET requests only accept ASCII or UTF-8 parameters but POST requests accept any form of data.

Also read:

How to Create Swap File in Linux
SFTP Script to Transfer Files in Linux with Password
How to Reverse String in Python
How to Connect to PostrgeSQL Database from Python
How to Remove Snap in Ubuntu

Leave a Reply

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