0
  • 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

enter image description here

sgn
  • 125
  • 2
  • 10
  • 1. According to your code `headers` are set automatically by `requests` and may different than `Postman` ones. 2. `data` and `files` set separately in `requests` so attachment processing is also different. Try to get [raw `request` data](https://stackoverflow.com/questions/10999990/get-raw-post-body-in-python-flask-regardless-of-content-type-header) in Flask and check what is actually different – rzlvmp Nov 03 '22 at 03:23

0 Answers0