I'll help you create a Python script that makes a multipart form request similar to the one you've shown. Here's a script that replicates that request structure:
import requests
# Define the URL where you want to send the request
url = "https://your-api-endpoint.com/upload" # Replace with your actual URL
# Create the multipart form data
files = {
'fileToUpload': ('video.mp4', open('video.mp4', 'rb'), 'video/mp4')
}
data = {
'time': '1h',
'fileNameLength': '16',
'reqtype': 'fileupload'
}
# Make the request
try:
response = requests.post(url, files=files, data=data)
# Print the response
print(f"Status Code: {response.status_code}")
print(f"Response Headers: {response.headers}")
print(f"Response Body: {response.text}")
except requests.exceptions.RequestException as e:
print(f"An error occurred: {e}")
finally:
# Make sure to close the file
files['fileToUpload'][1].close()
Here's an alternative version with more error handling and using a context manager for the file:
import requests
from pathlib import Path
def upload_file(file_path, url):
"""
Upload a file using multipart form data
Args:
file_path: Path to the file to upload
url: The endpoint URL
"""
# Check if file exists
if not Path(file_path).exists():
print(f"Error: File '{file_path}' not found")
return
# Prepare the multipart form data
with open(file_path, 'rb') as f:
files = {
'fileToUpload': (Path(file_path).name, f, 'video/mp4')
}
data = {
'time': '1h',
'fileNameLength': '16',
'reqtype': 'fileupload'
}
try:
# Make the POST request
response = requests.post(url, files=files, data=data)
response.raise_for_status() # Raise an exception for bad status codes
print(f"Upload successful!")
print(f"Status Code: {response.status_code}")
print(f"Response: {response.text}")
return response
except requests.exceptions.HTTPError as e:
print(f"HTTP Error: {e}")
except requests.exceptions.ConnectionError as e:
print(f"Connection Error: {e}")
except requests.exceptions.Timeout as e:
print(f"Timeout Error: {e}")
except requests.exceptions.RequestException as e:
print(f"Request Error: {e}")
# Usage
if __name__ == "__main__":
# Replace with your actual values
FILE_PATH = "video.mp4"
UPLOAD_URL = "https://your-api-endpoint.com/upload"
upload_file(FILE_PATH, UPLOAD_URL)
If you need to match the exact boundary format shown in your example, you can use a more low-level approach:
import requests
from requests_toolbelt import MultipartEncoder
# Create a multipart encoder with a specific boundary
multipart_data = MultipartEncoder(
fields={
'time': '1h',
'fileNameLength': '16',
'reqtype': 'fileupload',
'fileToUpload': ('video.mp4', open('video.mp4', 'rb'), 'video/mp4')
},
boundary='----geckoformboundary5f6f2e8b5fb6aa4fd0e725d3bbe8e57f'
)
# Make the request
response = requests.post(
'https://your-api-endpoint.com/upload',
data=multipart_data,
headers={'Content-Type': multipart_data.content_type}
)
print(response.status_code)
print(response.text)
Note: For the last example, you'll need to install the requests-toolbelt package:
pip install requests-toolbelt
Remember to:
- Replace
"https://your-api-endpoint.com/upload" with your actual API endpoint
- Make sure the file
video.mp4 exists in the same directory as your script, or provide the full path
- Install the required packages:
pip install requests (and requests-toolbelt if using the last example)