The requests library is the most popular way to make HTTP requests in Python. While GET requests are used to retrieve data, POST requests are used to send data to a server — such as submitting a form, uploading JSON, or interacting with REST APIs.
In this article, you’ll learn how to use requests.post() with clear examples you can adapt in your projects.
What is a POST Request in Python?
A POST request is an HTTP method that sends data to a server. In Python, the requests library makes POST requests easy by allowing you to send:
- Form data
- JSON payloads
- Files
Before you begin, install requests if you haven’t already:
pip install requests
Example 1 — Simple POST with Form Data
This example demonstrates how to send form-encoded data (like a web form) to an API.
import requests
url = "https://httpbin.org/post"
data = {"username": "alice", "password": "secret"}
response = requests.post(url, data=data)
print("Status Code:", response.status_code)
print("Response JSON:", response.json())
Output:
Status Code: 200
Response JSON:{'args': {}, 'data': '', 'fil...
Below is the prettified JSON result
{
"args":
{},
"data": "",
"files":
{},
"form":
{
"password": "secret",
"username": "alice"
},
"headers":
{
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate, br, zstd",
"Content-Length": "30",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.32.4",
"X-Amzn-Trace-Id": "Root=1-6994d2f2-63fd9a082d5400f169e5a4d9"
},
"json": 0,
"origin": "84.242.191.86",
"url": "https://httpbin.org/post"
}
Explanation:
data=sends form-encoded key–value pairsresponse.status_codeshows the HTTP statusresponse.json()parses the JSON response
This technique is useful for submitting login forms, search queries, and other web form interactions.
Example 2 — POST with JSON Payload
When interacting with modern APIs, you usually send JSON instead of form data.
import requests
url = "https://httpbin.org/post"
json_data = {"task": "write article", "priority": "high"}
response = requests.post(url, json=json_data)
print("Status:", response.status_code)
print("Echoed JSON:", response.json()["json"])
Status: 200
Echoed JSON: {'priority': 'high', 'task': 'write homework'}
Explanation:
json=automatically setsContent-Type: application/json- The server echoes the JSON back for validation
- This is ideal for REST API calls
Example 3 — POST Request with Proxy
In some cases (web scraping, corporate networks, geo-testing), you may need to send a POST request through a proxy server.
import requests
url = "https://httpbin.org/post"
data = {"query": "python requests proxy"}
proxies = {
"http": "http://XX.XX.XX.XX:8080",
"https": "http://XX.XX.XX.XX:8080"
}
response = requests.post(url, data=data, proxies=proxies, timeout=10)
print("Status Code:", response.status_code)
print("Response:", response.json())
result:
Status Code: 200
Response: {'args': {}, 'data': '', 'files': {}, 'form': {'query': 'python requests proxy'}, 'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate, br, zstd', 'Content-Length': '27', 'Content-Type': 'application/x-www-form-urlencoded', 'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.32.4', 'X-Amzn-Trace-Id': 'Root=1-6994d569-7d88c3c16f582a484c597f80'}, 'json': None, 'origin': '84.242.191.86', 'url': 'https://httpbin.org/post'}
Explanation
proxies=defines the proxy server for HTTP and HTTPS.- Replace
123.45.67.89:8080with your actual proxy IP and port. timeout=10prevents the request from hanging indefinitely.- Useful for scraping, security testing, and bypassing network restrictions.
Example with Proxy Authentication
If your proxy requires a username and password:
proxies = {
"http": "http://username:[email protected]:8080",
"https": "http://username:[email protected]:8080"
}
response = requests.post(url, json={"key": "value"}, proxies=proxies)
For list of free proxies you can check this repo: free-proxy-list
Quick Reference Table
| Task | Code Example |
|---|---|
| Basic POST | requests.post(url, data=data) |
| JSON POST | requests.post(url, json=data) |
| POST with proxy | requests.post(url, data=data, proxies=proxies) |
| Proxy with auth | http://user:pass@ip:port |
Common Use Cases for POST Requests
- Authentication: Login or token exchange
- API submission: Send data to services (e.g., create users, upload content)
- Form automation: Automated form filling
- File uploads: Send files to servers
- Proxy: Proxy usage and geolocation change
- JSON: JSON data exchange
Tips for Using requests.post()
- Always check
response.status_codebefore processing results. - For long waits, use
timeout=to avoid hanging requests. - Handle exceptions using
try/except requests.exceptions.RequestException. - Inspect headers with
response.headersfor debugging.