I am attempting to POST some files and parameters as Multi-Part in the following pattern. I've tried both dict and list. list
threw a HTTP 415
. So I went forward with dict
. I would like to post a few parameters as well as multiple files under a common name 'file' (along with their respective filenames).
The string parameters are working fine, the error was with the file upload
Data passed within the multi-part dict object
multipart = {
'param1': 'paramVal1',
'param2': 'paramVal2',
'file': (("file1.xml", ByteIO), ("file1.xml", ByteIO)),
}
POST using python-requests
requests.post(url='http://localhost:8888/upload',files=multipart)
Throws the error
File "C:\Python36\lib\site-packages\requests\api.py", line 107, in post
return request('post', url, data=data, json=json, **kwargs)
File "C:\Python36\lib\site-packages\requests\api.py", line 53, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Python36\lib\site-packages\requests\sessions.py", line 454, in request
prep = self.prepare_request(req)
File "C:\Python36\lib\site-packages\requests\sessions.py", line 388, in prepare_request
hooks=merge_hooks(request.hooks, self.hooks),
File "C:\Python36\lib\site-packages\requests\models.py", line 296, in prepare
self.prepare_body(data, files, json)
File "C:\Python36\lib\site-packages\requests\models.py", line 447, in prepare_body
(body, content_type) = self._encode_files(files, data)
File "C:\Python36\lib\site-packages\requests\models.py", line 142, in _encode_files
fn, fp, ft, fh = v
ValueError: too many values to unpack (expected 4)
Expected POST Body
------WebKitFormBoundaryZ7HAof4KTgAB21YV
Content-Disposition: form-data; name="file"; filename="file1.xml"
Content-Type: text/xml
------WebKitFormBoundaryZ7HAof4KTgAB21YV
Content-Disposition: form-data; name="file"; filename="file2.xml"
Content-Type: text/xml
------WebKitFormBoundaryZ7HAof4KTgAB21YV
Content-Disposition: form-data; name="param1"
paramVal1
------WebKitFormBoundaryZ7HAof4KTgAB21YV
Content-Disposition: form-data; name="param2"
paramVal2
------WebKitFormBoundaryZ7HAof4KTgAB21YV--