Python Requests - Send HTTP POST
Python - Send HTTP POST Request
HTTP POST request is used to create or update a resource in a specified server.
In Python Requests library, requests.post()
method is used to send a POST request to a server over HTTP. You can also send additional data in the POST request using data
parameter.
Examples
1. Send HTTP POST Request
In this example, we shall send a POST request to the server with the given URL.
Python Program
import requests
response = requests.post('https://example.com/api/', data = {'key': 'value'})
Explanation:
- The
requests.post()
function sends an HTTP POST request to the provided URL with the specified data. - The
data
parameter is a dictionary containing the key-value pairs you want to send to the server. - Once the request is sent, a Response object is returned, containing the server's response, including the status code, headers, and content.
Let us print the headers received in the response to the POST request made in the above example.
Python Program
import requests
response = requests.post('https://example.com/api/', data={'key1': 'value1', 'key2': 'value2'})
print(response.headers)
Output
{'Date': 'Mon, 25 Mar 2019 11:16:23 GMT', 'Content-Type': 'text/html; charset=UTF-8', 'Content-Length': '12136', 'Connection': 'keep-alive', 'Keep-Alive': 'timeout=30', 'Server': 'Apache/2', 'X-Powered-By': 'PHP/5.6.30', 'Link': '</wp-json/>; rel="https://api.w.org/", </>; rel=shortlink', 'Vary': 'Accept-Encoding', 'Content-Encoding': 'gzip', 'Referrer-Policy': 'no-referrer-when-downgrade'}
2. Send POST Request with JSON Data
POST requests are often used to send data to the server in JSON format. To send JSON data, you need to set the Content-Type
header to application/json
and pass the data as JSON.
Python Program
import requests
import json
headers = {'Content-Type': 'application/json'}
data = {'name': 'John', 'age': 30}
response = requests.post('https://example.com/api/', headers=headers, data=json.dumps(data))
print(response.json())
Explanation:
- Here, we send JSON data to the server by serializing the dictionary using
json.dumps()
. - The
headers
dictionary specifies that the content type is JSON (application/json
). - The server will process the request as JSON and return a JSON response, which we print using
response.json()
.
Output
{'status': 'success', 'message': 'Data received'}
3. Handling POST Request with Files
Another use case of POST requests is to upload files to the server. We can send a file as part of the POST request using the files
parameter.
Python Program
import requests
files = {'file': open('example.txt', 'rb')}
response = requests.post('https://example.com/upload', files=files)
print(response.status_code)
Explanation:
- The
files
parameter is used to send files in a POST request. We open the file in binary mode (rb
) and pass it as a dictionary with the file key. - The server processes the file and responds with a status code, which we print in the program.
Output
200
About HTTP POST Request
Following are some of the points to be noted while working with a POST request:
- POST requests are never cached.
- POST requests do not remain in the browser history.
- POST requests cannot be bookmarked.
- POST requests have no restrictions on data length.
Summary
In this tutorial, we learned how to send an HTTP POST request using the Python requests
library. We also explored various use cases such as sending form data, JSON data, and uploading files in a POST request.