Can't upload a document from python SDK

I have spent several hours trying to upload a customer document, but I only receive a bunch of errors from the server, mainly “Invalid file type” and crashing on token class trying to read a 400 response, but without the status field

I have a flask server and this is the code:

                file = io.BytesIO(document['document'].read())
                request_body = {
                    'file': file,
                    'documentType': document['document_type']
                }
                services.dwolla.token.post(document['url'], body=request_body)

this is the response
<Response [400]>
{‘code’: ‘ValidationError’, ‘message’: ‘Validation error(s) present. See embedded errors list for more details.’, ‘_embedded’: {‘errors’: [{‘code’: ‘Invalid’, ‘message’: ‘Invalid file type.’, ‘path’: ‘/file’, ‘_links’: {}}]}}

I guess the error is on how I’m sending the file, but I have tried a lot of ways, and none is working

Pls help! My app is in production

Hi David - thanks for posting!

The "Invalid file type." error is thrown whenever the uploaded file is not of one of these types - .jpg , .jpeg, .png and .pdf. Would you be able to check the file type of thee document you’re attempting to upload?

Hi Shreya, thx for your response.
I have tried them all.

Actually, with very small files (less than 500k), and using .getvalue() instead of read() it was working. I guess that problem is related to the multipart thing.

Also, pls notice that the error I’m getting is not the documented error {status:400, code: invalidFileType}

Dwolla token class is crashing because I’m receiving a JSON without status value.

Which object type is dwolla expecting for the file field?

The problem was the way Flask handle the FileStorage and File objects. Before sending the file it is required to set the pointer to the beginning of the file with the seek function:

        file_storage = document['document']
        file_storage.stream.seek(0)                    
        file = io.BytesIO(file_storage.read())