"Files are empty" 400 error on upload (Python)

I am trying to use the Upload plugin to upload a binary file using Python. This is my code

url = f"{strapi_server_url}/api/upload"
headers = {    'Authorization': f"Bearer {jwt}"}
filename = "my_filename.stl"
file=open(os.getcwd() + f'\\toys\\{filename}', 'rb')
#my_data = file.read()
#print(f"LENGTH FILE: {len(my_data)}")
payload={'submit': "Submit"}

response = requests.post(url, files={'file': (filename, file )}, data=payload, headers=headers)

I get the following response:

{'data': None, 'error': {'status': 400, 'name': 'ValidationError', 'message': 'Files are empty', 'details': {}}}

It says the files are empty, but when I uncomment the lines above it gives me the correct file size, making me think that the file is being read. Yet it says it is empty? It also says data=None, although I am passing in a dictionary.

From the docs, this is the JavaScript


<form>
  <!-- Can be multiple files -->
  <input type="file" name="files" />
  <input type="submit" value="Submit" />
</form>

<script type="text/javascript">
  const formElement = document.querySelector('form');
  formElement.addEventListener('submit', (e) => {
    e.preventDefault();
    const request = new XMLHttpRequest();
    request.open('POST', '/upload');
    request.send(new FormData(formElement));
  });

</script>

✋ CAUTION

You have to send FormData in your request body.

I notice the example is using XMLHttpRequest while I am using multipart/form-data.
Any tips? Thanks for your responses.

System Information
  • Strapi Version: 4.0.0
  • Operating System: Ubuntu 20.04.3 LTS
  • Database: PG 13.4
  • Node Version: v12.22.7
  • NPM Version: 6.14.15
  • Yarn Version: 1.22.17

I used files={‘file’: filename.stl}
It should be files={'files’: filename.stl}

For reference, the code below allowed me to upload a binary file to Strapi Uploads using the Python requests library.

url = f"{strapi_server_url}/api/upload"
headers = {
    'Authorization': f"Bearer {jwt}",
}
filename = "filename.stl"
file={'files': open(Path(os.getcwd(), filename), 'rb')}
payload={'submit': "Submit"}
response = requests.post(url, files=file, data=payload, headers=headers)