0

Having following code for POST request with file attached:

        file = open(file_name, 'rb')
        files = {'file': file}

        import requests
        res = requests.request(method='POST', url=port_url, files=files, data=params)

file here of type <class '_io.BufferedReader'>.

Now I am trying to impliment similar with file body received by me from incoming email attachment as bytes array (want to avoid storing file to local disk):

        file = file_body
        files = {'file': file}

        import requests
        res = requests.request(method='POST', url=port_url, files=files, data=params)

and it doesn't work for some reason. Not sure what is wrong actually but external service (CloudConvert) I am trying to send unable to recognize file correctly. Here file of type <class 'bytes'>.

Tried to convert it to BytesIo or BufferedReader as suggested here without luck.

Could someone help me to post file taken from bytes array with requests.request?

user3583807
  • 766
  • 1
  • 8
  • 26
  • 2
    Show us your BytesIO attempt. BytesIO should be the way to go. – user2357112 Jan 01 '22 at 23:02
  • By adding `file.name = file_name` after `file = io.BytesIO(file_body)` it started to work. E.g. cloudconvert service I am using needs proper file name to work correctly. Probably it checks for extension. Thank you, Monica! – user3583807 Jan 01 '22 at 23:44
  • documentation for [API](https://cloudconvert.com/api/v2#overview) shows special module for this: [cloudconvert-python](https://github.com/cloudconvert/cloudconvert-python) – furas Jan 02 '22 at 01:21

0 Answers0