Python : Trying to POST form using requests
Categories:
Mastering POST Requests with Python's Requests Library

Learn how to effectively send POST requests with various data types (form-encoded, JSON, files) using the Python requests
library.
The requests
library in Python is a powerful and user-friendly HTTP library for making web requests. While GET requests are straightforward for retrieving data, POST requests are essential for sending data to a server, such as submitting forms, uploading files, or creating new resources. This article will guide you through the process of constructing and sending various types of POST requests using requests
.
Understanding POST Request Basics
A POST request sends data to a server to create or update a resource. Unlike GET requests, which append data to the URL, POST requests include data in the request body. The requests
library simplifies this by allowing you to pass data as dictionaries, strings, or file-like objects, and it intelligently handles the content type headers.
sequenceDiagram participant Client participant Server Client->>Server: POST /submit_form HTTP/1.1 Client->>Server: Content-Type: application/x-www-form-urlencoded Client->>Server: name=Alice&email=alice@example.com Server-->>Client: HTTP/1.1 200 OK Server-->>Client: Response Body (e.g., success message)
Sequence diagram of a basic form submission via POST request.
Sending Form-Encoded Data
The most common use case for POST requests is submitting HTML forms. When a form is submitted with method="POST"
and enctype="application/x-www-form-urlencoded"
, the data is sent as key-value pairs in the request body. The requests
library handles this automatically when you pass a dictionary to the data
parameter.
import requests
url = 'https://httpbin.org/post' # A service that echoes POST requests
payload = {
'username': 'testuser',
'password': 'testpassword',
'email': 'test@example.com'
}
try:
response = requests.post(url, data=payload)
response.raise_for_status() # Raise an exception for bad status codes
print(f"Status Code: {response.status_code}")
print(f"Response JSON: {response.json()}")
except requests.exceptions.RequestException as e:
print(f"An error occurred: {e}")
Example of sending form-encoded data using requests.post()
.
requests
automatically sets the Content-Type
header to application/x-www-form-urlencoded
. You typically don't need to set this manually.Sending JSON Data
Many modern APIs expect data in JSON format. The requests
library provides a convenient json
parameter for this. When you pass a dictionary to the json
parameter, requests
automatically serializes it to a JSON string and sets the Content-Type
header to application/json
.
import requests
import json
url = 'https://httpbin.org/post'
json_payload = {
'name': 'Jane Doe',
'age': 30,
'isStudent': False,
'courses': ['Math', 'Science']
}
try:
response = requests.post(url, json=json_payload)
response.raise_for_status()
print(f"Status Code: {response.status_code}")
print(f"Response JSON: {response.json()}")
except requests.exceptions.RequestException as e:
print(f"An error occurred: {e}")
Example of sending JSON data using the json
parameter.
data
parameter and manually set the Content-Type
header to application/json
.Uploading Files
Uploading files via POST requests is handled using the files
parameter. This parameter expects a dictionary where keys are the field names (as expected by the server) and values can be a tuple (filename, file_object, content_type)
or simply a file-like object.
import requests
url = 'https://httpbin.org/post'
# Create a dummy file for demonstration
with open('my_document.txt', 'w') as f:
f.write('This is the content of my document.')
try:
with open('my_document.txt', 'rb') as f:
files = {'upload_file': f}
response = requests.post(url, files=files)
response.raise_for_status()
print(f"Status Code: {response.status_code}")
print(f"Response JSON: {response.json()}")
except requests.exceptions.RequestException as e:
print(f"An error occurred: {e}")
# Clean up the dummy file
import os
os.remove('my_document.txt')
Example of uploading a file using the files
parameter.
requests
automatically sets the Content-Type
header to multipart/form-data
. Ensure your server is configured to handle this content type for file uploads.Handling Headers and Authentication
You can customize request headers using the headers
parameter, which takes a dictionary of header names and values. For authentication, requests
supports various methods, including basic authentication via the auth
parameter.
import requests
url = 'https://httpbin.org/post'
custom_headers = {
'User-Agent': 'MyPythonApp/1.0',
'X-Custom-Header': 'Value'
}
# Basic Authentication
auth_tuple = ('user', 'pass')
try:
response = requests.post(url, data={'key': 'value'}, headers=custom_headers, auth=auth_tuple)
response.raise_for_status()
print(f"Status Code: {response.status_code}")
print(f"Response JSON: {response.json()}")
except requests.exceptions.RequestException as e:
print(f"An error occurred: {e}")
Adding custom headers and basic authentication to a POST request.
By understanding these different ways to construct POST requests, you can effectively interact with a wide range of web services and APIs using Python's requests
library.