1

For example: I need to post data in such a format: {imgFiles:[(filename, file), (filename, file), (flename file)]

I have tried to do it like this:

pic_array = [
    ('file1', open("somefile.xml", "r")),
    ('file2', open("somefile2.xml", "r"))
]
files_pics = [('imgFiles', pic_array)]

r = requests.post(
    'https://some.site/path/to/api/point',
    data=data_details,
    headers=headers_1,
    files=files_pics
)
print(r.status_code, r.reason, r.json())

and get a

 Traceback (most recent call last):
 File "C:/Users/someusername/PycharmProjects/someprojectname/data_load.py", line 115, in <module>

 File "C:\ProgramData\Anaconda3\lib\site-packages\requests\api.py", line 116, in post
return request('post', url, data=data, json=json, **kwargs)
 File "C:\ProgramData\Anaconda3\lib\site-packages\requests\api.py", line 60, in request
return session.request(method=method, url=url, **kwargs)
 File "C:\ProgramData\Anaconda3\lib\site-packages\requests\sessions.py", line 519, in request
prep = self.prepare_request(req)
 File "C:\ProgramData\Anaconda3\lib\site-packages\requests\sessions.py", line 462, in prepare_request
hooks=merge_hooks(request.hooks, self.hooks),
 File "C:\ProgramData\Anaconda3\lib\site-packages\requests\models.py", line 316, in prepare
self.prepare_body(data, files, json)
 File "C:\ProgramData\Anaconda3\lib\site-packages\requests\models.py", line 504, in prepare_body
(body, content_type) = self._encode_files(files, data)
 File "C:\ProgramData\Anaconda3\lib\site-packages\requests\models.py", line 151, in _encode_files
fn, fp, ft, fh = v
ValueError: not enough values to unpack (expected 4, got 1)

Is there any way to post files exactly as array?

2
  • This error message is meaningless without context. Please post the full traceback Commented Feb 11, 2019 at 13:41
  • Updated with a full traceback Commented Feb 11, 2019 at 20:26

1 Answer 1

2

You are sending multiple files incorrectly see the following working example. If I take my example and change it to a list like yours I get the same error.

import requests

url = 'http://localhost:8080/'
files = {'file':  open('sql.py', 'rb'),
         'file2': open('lst.py', 'rb')}

r = requests.post(url, files=files)
print(r.text)
Sign up to request clarification or add additional context in comments.

1 Comment

The point is the server API accepts files ONLY in this format: {key1:value1,key2:value2,imgFiles:[(filename,file), (filename2,file2), .., (filenameN,fileN)]}

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.