Using requests 2.4.3. I want to post a file and send a few fields from a form to my flask server. I already have an angular app that validates flask is working btw.
Based on various threads on the subject I've tried a few approaches to no avail. Here's what I've tried and the output:
a) Putting everything in the files map
data = {'formfoo': 'whatever'}
files = {'test.csv': open(fpath, 'rb'), 'data': json.dumps(data)}
resp = super(Session, self).post(url,
files=files,
verify=False,
headers=multipart)
## BRINGS THIS ERROR ###
File "../2.7/lib/python2.7/json/decoder.py", line 383, in raw_decode
raise ValueError("No JSON object could be decoded")
b) Separately pass data and files to the post API data = {'formfoo': 'whatever'} files = {'test.csv': open(fpath, 'rb')} resp = super(Session, self).post(url, data=data, files=files, verify=False, headers=multipart)
## BRINGS THIS ERROR. Same as (a) ###
File "../2.7/lib/python2.7/json/decoder.py", line 383, in raw_decode
raise ValueError("No JSON object could be decoded")
c) Separately pass data and files to the post API. json.dumps the data dictionary like a usual post. data = {'formfoo': 'whatever'} files = {'test.csv': open(fpath, 'rb')} resp = super(Session, self).post(url, data=json.dumps(data), files=files, verify=False, headers=multipart)
## BRINGS THIS ERROR ###
File "./venv/lib/python2.7/site-packages/requests/models.py", line 114, in _encode_files
raise ValueError("Data must not be a string.")
ValueError: Data must not be a string
Either way the form data is not received by the server because the client encoding fails. Any suggestions?