How to send POST Requests with Python Requests Library
Sending POST requests in Python is simplified with the requests library, a popular and user-friendly tool for handling HTTP operations. This guide walks you through sending POST requests, using the library efficiently, and exploring its versatile features.
How to Send a POST Request in Python?
To send a POST request, use the requests.post() method, providing:
- The target URL as the first parameter.
- POST data via the data= parameter.
For the server to process the data correctly, set the Content-Type header in the request. If not specified, it defaults to application/x-www-form-urlencoded. Here’s a simple example:
import requests
url = "https://reqbin.com/echo/post/json"
payload = {"key": "value"}
response = requests.post(url, data=payload)
print(response.text)
What is the Python Requests Library?
The requests library is a widely-used Python module for making HTTP requests. It simplifies handling methods like POST, GET, DELETE, and more. With requests, you can:
- Submit forms.
- Upload files or images.
- Send JSON or XML data.
Compared to Python’s built-in urllib3 module, requests is more user-friendly and requires less boilerplate code.
What is HTTP POST?
The HTTP POST method is used to:
- Submit data to a server.
- Upload files.
- Change server states.
POST differs from GET in that it can modify server-side resources and is not idempotent, meaning repeated requests may have different effects.
How to Get Started with the Python Requests Library
Installation:
Install the library using pip:
pip install requests
Or, with Pipenv:
pipenv install requests
Importing Requests:
Once installed, use this in your Python script:
import requests
Using the POST Method with Python Requests
POST Request Syntax
requests.post(url, data=data, json=json, headers=headers)
Example: Submit an HTML Form
url = "https://reqbin.com/echo/post/form"
form_data = {"name": "Dharmender", "age": "30"}
response = requests.post(url, data=form_data)
print(response.status_code)
Advanced Features of Python POST Requests
POST JSON Data
To send JSON data, use the json= parameter. The library will automatically set the Content-Type: application/json header.
payload = {"name": "Dharmender", "job": "Developer"}
response = requests.post(url, json=payload)
Upload a File
Send files using the files= parameter:
files = {'file': open('example.txt', 'rb')}
response = requests.post(url, files=files)
Upload Multiple Files
For multiple files, pass a list of file tuples:
files = [('file1', open('example1.txt', 'rb')), ('file2', open('example2.txt', 'rb'))]
response = requests.post(url, files=files)
Custom Headers
To include custom headers, use the headers= parameter:
headers = {"Authorization": "Bearer token123"}
response = requests.post(url, headers=headers)
Send Cookies
Include cookies with the cookies= parameter:
cookies = {'session_id': '12345'}
response = requests.post(url, cookies=cookies)
Authentication
Send credentials using the auth= parameter:
from requests.auth import HTTPBasicAuth
response = requests.post(url, auth=HTTPBasicAuth('username', 'password'))
With its ease of use and robust functionality, the requests library stands out as the go-to tool for handling HTTP requests in Python. Whether you need to upload files, send JSON data, or handle authentication, this library has you covered.
Keep Learning 🙂