Below is a Python script that uses the requests library to send a single HTTP POST request mimicking the multipart/form-data upload you described. It includes:
- The form fields (
time, fileNameLength, reqtype).
- The file upload (
fileToUpload with filename video.mp4 and content type video/mp4).
Assumptions:
- URL: The original message doesn't specify the target URL. I've assumed
http://example.com/upload as a placeholder. Replace it with the actual URL (e.g., from your server's endpoint).
- File Content: The original message doesn't include the actual binary content of
video.mp4. The script assumes you have a local file named video.mp4 in the same directory as the script. If not, replace the file path or provide dummy content (e.g., an empty file). If you need to generate dummy video content, I can add that.
- Boundary: The
requests library automatically generates a random boundary (similar to the "geckoformboundary" in your example). This is standard and should work unless your server strictly validates the exact boundary string (which is rare). If you need an exact custom boundary, let me know for a more advanced raw-body implementation.
- Headers: I've added a
Content-Type header with multipart/form-data, but requests handles this automatically when using files.
- Dependencies: Install
requests if needed: pip install requests.
Python Script
import requests
# Target URL (replace with your actual upload endpoint)
url = 'http://example.com/upload' # e.g., 'https://yourserver.com/upload'
# Form data fields
data = {
'time': '1h',
'fileNameLength': '16',
'reqtype': 'fileupload'
}
# File to upload (assumes 'video.mp4' exists in the current directory)
# Replace 'video.mp4' with your actual file path if needed.
# The tuple format is: (filename, file_object, content_type)
files = {
'fileToUpload': ('video.mp4', open('video.mp4', 'rb'), 'video/mp4')
}
# Optional: Add headers if needed (e.g., for authentication or custom boundary simulation)
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0' # Mimic Firefox if needed
}
# Send the POST request
try:
response = requests.post(url, data=data, files=files, headers=headers)
# Print response details
print(f'Status Code: {response.status_code}')
print(f'Response Headers: {response.headers}')
print(f'Response Content: {response.text[:500]}') # First 500 chars for brevity
except requests.exceptions.RequestException as e:
print(f'Error during request: {e}')
# Clean up: Close the file handle (optional, but good practice)
for field in files.values():
if hasattr(field[1], 'close'):
field[1].close()
How It Works:
- Form Data: The
data dictionary sends the non-file fields as part of the multipart request.
- File Upload: The
files dictionary handles the file attachment. It opens video.mp4 in binary mode ('rb') and specifies the filename and MIME type.
- Request:
requests.post combines everything into a single multipart/form-data POST request. The library automatically generates the boundary and structures the body similar to your example (e.g., Content-Disposition for each part).
- Error Handling: Basic try-except to catch network errors.
- Output: Prints the response status, headers, and partial content for debugging.
Example Output (if successful):
Status Code: 200
Response Headers: {'Content-Type': 'text/html', ...}
Response Content: <html><body>Upload successful!</body></html>
Testing and Customization:
-
Run the Script: Save it as upload_script.py and run python upload_script.py.
-
If No Local File: If you don't have video.mp4, create a dummy one:
# Add this before the 'files' dictionary to create a dummy empty file
with open('video.mp4', 'wb') as f:
f.write(b'Dummy video content') # Or actual binary data
-
Exact Boundary Matching: If you must use the exact boundary from your example (geckoformboundary5f6f2e8b5fb6aa4fd0e725d3bbe8e57f), we can craft the raw multipart body manually using multipart encoding. Here's a quick alternative snippet for that (requires requests and email for MIME handling):
import requests
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
boundary = 'geckoformboundary5f6f2e8b5fb6aa4fd0e725d3bbe8e57f'
msg = MIMEMultipart(boundary=boundary)
msg.attach(MIMEText('1h', 'time'))
msg.attach(MIMEText('16', 'fileNameLength'))
msg.attach(MIMEText('fileupload', 'reqtype'))
# Attach file
part = MIMEBase('video', 'mp4')
with open('video.mp4', 'rb') as f:
part.set_payload(f.read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', 'form-data; name="fileToUpload"; filename="video.mp4"')
msg.attach(part)
headers = {'Content-Type': f'multipart/form-data; boundary={boundary}'}
response = requests.post(url, data=msg.as_string(), headers=headers)
print(response.text)
If this doesn't match your needs (e.g., wrong URL, need dummy file content, or specific error handling), provide more details!