For Strapi, I am given this JavaScript boilerplate to upload a file from a form. For more information see here: https://docs.strapi.io/developer-docs/latest/plugins/upload.html#upload-files-related-to-an-entry
const formElement = document.querySelector('form');
formElement.addEventListener('submit', (e) => {
e.preventDefault();
const request = new XMLHttpRequest();
request.open('POST', '/upload');
request.send(new FormData(formElement));
});
I am writing a Python program. How do I translate this in Python?. I am not using a form, I am pulling the file from a location on the drive.
But there is a warning in the documentation: You have to send FormData in your request body.
This is what I have so far:
url = f"{strapi_server_url}/api/upload"
headers = {
'Authorization': f"Bearer {jwt}",
}
file=open(os.getcwd() + f'\\toys\\{filename}', 'rb')
data = file.read()
print(f"LENGTH FILE: {len(data)}")
response = requests.post(url,files={'file': data}, headers=headers)
I get a 400 error. 'ValidationError': 'Files are empty'. But I verified the length of the 'data' variable: 5 MB.