2

Iam trying to upload a picture and a information over an API that requires to be send as a form. I tried to use the "files" option, that requests provides with no success. It gives me the following error:

AttributeError: 'int' object has no attribute 'read'

The line of code I tried is:

r = requests.post(url, headers=header, files = {'imageFile' :    open('test_pic/1.jpg'), 'ticket' : ticket}, verify=False)

Cheers Florian

0

2 Answers 2

2
files = {'imageFile' :    open('test_pic/1.jpg'), 'ticket' : ticket}

Is the ticket of type int? I just got the same problem, the value in files must be str or bytes or bytearray or a file object(this will cause a read action),see details in requests's models.py(function _encode_files())

Sign up to request clarification or add additional context in comments.

Comments

1

There are a couple of things to try:

  1. If you're using Windows make sure to add a b to the file permissions for open:

    open('filename', 'rb')
    

    This will make sure that the file is read as a binary which otherwise can cause some errors

  2. When sending multiple files, you need to pass in a list of tuples, and not a dictionary:

    >>> multiple_files = [('images', ('foo.png', open('foo.png', 'rb'), 'image/png')),
                  ('images', ('bar.png', open('bar.png', 'rb'), 'image/png'))]
    >>> r = requests.post(url, files=multiple_files)
    

    This is according to the online documentation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.