- I have a very basic Flask implementation
- When making the request in Postman, I can get the form data but not the file I am trying to upload.
- I use the Code Generator in Postman, I copy and paste the code and make the same request using Python Requests, I am able to get both the form data and the file I am uploading.
- I have gone through a number of posts about Request.files being empty but nothing that I think helps.
- I can upload the same file using the same headers (in Postman), when using another web server.
This is the code I use (generated in Postman):
import requests
url = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
payload={
'Item': 'Test Item',
}
files=[
('filedata',('1.jpg',open('C:/temp/1.jpg','rb'),'image/jpeg'))
]
headers = {
'Authorization': 'Basic XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
}
response = requests.request("POST", url, headers=headers, data=payload, files=files)
print(response.text)
This is the server:
@claim.route('/account/<account>/environment/<env>/attachments', methods=['POST','OPTIONS'])
def upload_attachment(account, env): try: Form = [] for form in request.form: Form.append(form)
Files = []
for uploaded_file in request.files:
file = request.files[uploaded_file]
file_name = file.filename
Files.append(uploaded_file)
Files.append(file_name)
response = {
'Form': Form,
'Files': Files
}
return response