How to Upload Multiple Files to pdfRest with Python
In this tutorial, we'll use Python and the Upload Files API Tool to upload multiple files to pdfRest in one multipart request. The complete Upload Multiple to pdfRest repository sample stays visible while we explain the request fields and response behavior a Python application must adapt safely.
Why Upload Multiple Files to pdfRest with Python?
Batch-oriented Python applications may need to stage a related collection before they can merge, compare, or transform it. One Upload Files request can transfer the collection and return managed IDs for each member.
A research archive could upload a report, source image, and data appendix together, then associate each response entry with the ingest record. Later jobs can reference the right ID without reopening the original local file.
The sample creates an array of repeated ("file", file_tuple) entries because a dictionary cannot represent the same multipart field more than once. MultipartEncoder serializes that ordered list with the correct boundary.
Python Code Example
from requests_toolbelt import MultipartEncoder
import requests
import json
# By default, we use the US-based API service. This is the primary endpoint for global use.
api_url = "https://api.pdfrest.com"
# For GDPR compliance and enhanced performance for European users, you can switch to the EU-based service by uncommenting the URL below.
# For more information visit https://pdfrest.com/pricing#how-do-eu-gdpr-api-calls-work
#api_url = "https://eu-api.pdfrest.com"
upload_endpoint_url = api_url+'/upload'
# The /upload endpoint can take one or more files or urls as input and transfers them to the pdfRest server for processing.
# This sample takes 3 files and uploads it to the pdfRest service.
upload_request_data = []
# Array of tuples that contains information about the file that will be uploaded to the pdfRest server.
# The 'application/pdf' string below is known as a MIME type, which is a label used to identify the type of a file so that it is handled properly by software.
# Please see https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types for more information about MIME types.
files = [
('file_name.pdf', open('/path/to/file', 'rb'), 'application/pdf'),
('file_name2.pdf', open('/path/to/file', 'rb'), 'application/pdf'),
('file_name.jpg', open('/path/to/file', 'rb'), 'image/jpeg')
]
# Structure the data that will be sent to POST upload request as an array of tuples
for i in range(len(files)):
upload_request_data.append(("file", files[i]))
mp_encoder_upload = MultipartEncoder(
fields=upload_request_data
)
# Let's set the headers that the upload endpoint expects.
# Since MultipartEncoder is used, the 'Content-Type' header gets set to 'multipart/form-data' via the content_type attribute below.
headers = {
'Accept': 'application/json',
'Content-Type': mp_encoder_upload.content_type,
'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' # place your api key here
}
print("Sending POST request to upload endpoint...")
response = requests.post(upload_endpoint_url, data=mp_encoder_upload, headers=headers)
print("Response status code: " + str(response.status_code))
if response.ok:
response_json = response.json()
print(json.dumps(response_json, indent = 2))
else:
print(response.text)
Source for Upload Multiple to pdfRest: View the Python sample on GitHub.
Breaking Down the Code
Import the HTTP and multipart helpers
from requests_toolbelt import MultipartEncoder import requests import json
requests handles the HTTP exchange for Upload Multiple to pdfRest, while MultipartEncoder constructs form-data bodies with a boundary that the request header can reuse. The remaining Python modules support the response or timing behavior used specifically by Upload Multiple to pdfRest.
Select the pdfRest service region
# By default, we use the US-based API service. This is the primary endpoint for global use. api_url = "https://api.pdfrest.com" # For GDPR compliance and enhanced performance for European users, you can switch to the EU-based service by uncommenting the URL below. # For more information visit https://pdfrest.com/pricing#how-do-eu-gdpr-api-calls-work #api_url = "https://eu-api.pdfrest.com" upload_endpoint_url = api_url+'/upload'
The Python configuration keeps the pdfRest hostname separate from each Upload Multiple to pdfRest route. Using one regional base URL throughout this Python Upload Multiple to pdfRest job keeps its returned identifiers available to later requests.
Create the ordered repeated-file fields
upload_request_data = []
# Array of tuples that contains information about the file that will be uploaded to the pdfRest server.
# The 'application/pdf' string below is known as a MIME type, which is a label used to identify the type of a file so that it is handled properly by software.
# Please see https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types for more information about MIME types.
files = [
('file_name.pdf', open('/path/to/file', 'rb'), 'application/pdf'),
('file_name2.pdf', open('/path/to/file', 'rb'), 'application/pdf'),
('file_name.jpg', open('/path/to/file', 'rb'), 'image/jpeg')
]
# Structure the data that will be sent to POST upload request as an array of tuples
for i in range(len(files)):
upload_request_data.append(("file", files[i]))
mp_encoder_upload = MultipartEncoder(
fields=upload_request_data
)
# Let's set the headers that the upload endpoint expects.
# Since MultipartEncoder is used, the 'Content-Type' header gets set to 'multipart/form-data' via the content_type attribute below.
Each local source is added by Python under another file part in the same multipart body. The Upload Multiple to pdfRest response returns several resources whose filenames and IDs the Python caller should keep paired.
Send the encoded multipart request
headers = {
'Accept': 'application/json',
'Content-Type': mp_encoder_upload.content_type,
'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' # place your api key here
}
print("Sending POST request to upload endpoint...")
response = requests.post(upload_endpoint_url, data=mp_encoder_upload, headers=headers)
print("Response status code: " + str(response.status_code))
if response.ok:
The Python request joins the Upload Multiple to pdfRest endpoint, authentication header, and prepared body. For this Upload Multiple to pdfRest call, the multipart metadata generated by Python must remain paired with the body so pdfRest can separate files from options.
Inspect the upload response
if response.ok:
response_json = response.json()
print(json.dumps(response_json, indent = 2))
else:
print(response.text)
The final Upload Multiple to pdfRest handling in Python exposes the HTTP result and JSON body. Before using a Upload Multiple to pdfRest output or request ID, production Python code should verify the status and retain the identifier required by the next step.
Beyond the Tutorial
You now know how Python can send several files in one multipart body and preserve the resource information returned for each upload.
Open files with controlled lifetimes in long-running services and validate that the response count matches the request count. Store IDs with filenames and clean up temporary resources when downstream processing has finished.
Try the Upload Multiple to pdfRest workflow with a representative file in API Lab, then adapt the request in Python. The Upload Files API Tool documentation provides the complete Python context for Upload Multiple to pdfRest values, defaults, and limitations.