How do I upload binary files using Python?

System Information
  • Strapi Version: 4.0
  • Operating System: Windows
  • Database: PG
  • Node Version:
  • NPM Version:
  • Yarn Version:

on the Upload documentation here: Upload - Strapi Developer Docs

The docs give a code snippet for uploading a binary file in JavaScript.

<script type="text/javascript">
  const formElement = document.querySelector('form');

  formElement.addEventListener('submit', (e) => {

    const request = new XMLHttpRequest();'POST', '/upload');

    request.send(new FormData(formElement));

How do I do it in Python?

This is what I have so far:

url = f"{strapi_server_url}/api/upload"

headers = {
    'Authorization': f"Bearer {jwt}",

file = open(os.getcwd() + f'\\{filename}.stl', 'rb')
data =
print(f"LENGTH FILE: {len(data)}")

response =, files={'file': data}, headers=headers)

I get the error “Files are empty” But I know that the data variable has a length of 5 MB.

Alternatively, is there a way to upload binary files using CURL?