How to Upload a Single Binary File to pdfRest with Python

Learn how to use Python to upload a single binary file by calling Upload Files API Tool by pdfRest.
Share this page

Why Use Upload Files with Python?

The pdfRest Upload Files API Tool is a powerful resource for developers who need to upload files to a server for processing. This API provides an easy-to-use interface for transferring files such as PDFs or images to the pdfRest server, where they can be manipulated or transformed according to the user's needs.

This tutorial will guide you through the process of sending an API call to Upload Files using Python, a popular programming language known for its simplicity and readability.

In a business context, the ability to programmatically upload files can be crucial. For instance, a company might need to batch process a large number of invoices, convert scanned documents into searchable PDFs, or integrate file upload capabilities into their internal systems or customer-facing applications. By automating this process with the pdfRest API and Python, businesses can save time, reduce errors, and increase efficiency.

Upload Files with Python Code Example

import requests
import json

with open('/path/to/file', 'rb') as f:
    upload_data = f.read()

print("Uploading file...")
upload_response = requests.post(url='https://api.pdfrest.com/upload',
                    data=upload_data,
                    headers={'Content-Type': 'application/octet-stream', 'Content-Filename': 'file.pdf', "API-Key": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"})

print("Upload response status code: " + str(upload_response.status_code))

if upload_response.ok:
    upload_response_json = upload_response.json()
    print(json.dumps(upload_response_json, indent = 2))

else:
    print(upload_response.text)

Source of the provided code: GitHub

Breaking Down the Code

The provided code is an example of how to upload files to the pdfRest server using a multipart/form-data request in Python. Let's break down the key parts of the code:

upload_endpoint_url = 'https://api.pdfrest.com/upload'

This is the API endpoint URL to which the files will be uploaded.

files = [
    ('file_name.pdf', open('/path/to/file', 'rb'), 'application/pdf'),
    ...
]

Here we define the files to be uploaded. Each file is represented as a tuple containing the file name, a file object opened in binary mode, and the MIME type.

for i in range(len(files)):
    upload_request_data.append(("file", files[i]))

This loop constructs the data payload for the POST request, appending each file to the `upload_request_data` list.

headers = {
    'Accept': 'application/json',
    'Content-Type': mp_encoder_upload.content_type,
    'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
}

The headers include the expected 'Content-Type' set by the `MultipartEncoder`, the 'Accept' header indicating we expect a JSON response, and the 'Api-Key' which should be replaced with your actual API key from pdfRest.

response = requests.post(upload_endpoint_url, data=mp_encoder_upload, headers=headers)

This line sends the POST request to the upload endpoint with the data and headers we defined earlier.

Beyond the Tutorial

By following the tutorial above, you've learned how to upload files to the pdfRest server using Python. This is a fundamental step in automating the process of file handling and manipulation in your applications. With this knowledge, you can now explore other capabilities of the pdfRest API, such as converting, merging, or securing PDF files.

To further your understanding and capabilities, feel free to demo all of the pdfRest API Tools in the API Lab. For a comprehensive guide to the API, refer to the API Reference Guide.

Generate a self-service API Key now!
Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.