简体   繁体   中英

python requests api - http multipart post with file and form-data

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?

requests/2.4.3 works as expected:

#!/usr/bin/env python
import requests

response = requests.post('http://httpbin.org/post', 
    files={"file":open("file", "rb")}, 
    params={'param':'param data'},
    data={"data": "data data"})
response.raise_for_status()
print(response.json())

Request

POST /post?param=param+data HTTP/1.1
Host: httpbin.org
Content-Length: 238
Connection: keep-alive
Accept: */*
Accept-Encoding: gzip, deflate
User-Agent: python-requests/2.4.3 
Content-Type: multipart/form-data; boundary=0f34a3cfc1b448e78ef2bef3176d8948

--0f34a3cfc1b448e78ef2bef3176d8948
Content-Disposition: form-data; name="data"

data data
--0f34a3cfc1b448e78ef2bef3176d8948
Content-Disposition: form-data; name="file"; filename="file"

data
--0f34a3cfc1b448e78ef2bef3176d8948--

Output

{'files': {'file': 'data'},
 'form': {'data': 'data data'},
 'url': 'http://httpbin.org/post?param=param+data',
 'args': {'param': 'param data'},
 'data': '',
 'headers': {'Content-Length': '238',
  'Connection': 'close',
  'Host': 'httpbin.org',
  'User-Agent': 'python-requests/2.4.3',
  'X-Request-Id': '7afbf052-98f0-4e5e-8c6f-b16cfbfacbe9',
  'Content-Type': 'multipart/form-data; boundary=0f34a3cfc1b448e78ef2...',
  'Accept-Encoding': 'gzip, deflate',
  'Accept': '*/*'},
 'json': None}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM