Requests Post method:
The post is an HTTP method of requests module in python. HTTP method either sends the data or receive the data from the server. So, it acts as a bridge between a client and a server.
The post() method of requests module sends a POST request to a server. That means we can use post method when you want to send some data to the server. Moreover, the data sent by the POST method is not visible in the URL, so it is suitable to pass sensitive information. The POST requests are not stored in the browser history. We can also pass text file and binary file using POST method.
Method Signature
The signature for the post method is as shown below.
requests.post(url, data={key: value}, json={key: value}, args)
Method parameters and return type
- Here, the post method takes four parameters as listed below.
- url – Mandatory parameter. It specifies the URL
- data – It sends a dictionary, list of tuples, bytes to send as a query string. This is an optional parameter.
- json – It sends a JSON object to specified url. This is an optional parameter.
- args – It can pass several arguments like files, cookies, headers, proxies, timeout, stream, auth and cert. These arguments are optional.
- However, it returns a response object.
Python post method Examples:
Example 1: In this case, we will send requests to a URL and print the status code of its response. However, it will return code 200 on success.
#Importing requests module
import requests
# variable storing url
url="http://nhk.e6a.mytemp.website/python"
# Making a POST request
r = requests.post(url)
# Checking status code of response
# success code - 200
# Error code - 404/500 (apart from 200)
print("Status code - ",r.status_code)
Output
Status code - 200
Example 2: In this case,we will use the header parameter to set the HTTP headers on the given URL. Here, we are also sending an image file in its binary format with the files argument. It will raise an exception if file is not present. However, it will return code 200 on success.
#Importing requests module
import requests
# variable storing url
url = "http://nhk.e6a.mytemp.website/python"
#Using the headers parameter to set the HTTP headers:
x = requests.post(url, headers = {"HTTP_HOST": "TUTORIAL"})
print("Status code - ",x.status_code)
#Sending the file
f1 = {'file': open('Image100.jpg' ,'rb')}
x = requests.post(url, files=f1)
print("Status code - ",x.status_code)
Output
Status code - 200
Status code - 200
Example 3: In this case, it will return the status code if the response is received before timeout. However, it will raise ReadTimeout Exception, if the response is not received before timeout. However, it will return code 200 on success.
#Importing requests module
import requests
# variable storing url
url = "http://nhk.e6a.mytemp.website/python"
#Using the timeout parameter
x = requests.post(url, timeout=10)
print("Status code - ",x.status_code)
Output
Status code - 200
Conclusion
Hence, the post() method is used when we need to send some data to the server.
References
Happy Learning 🙂