title: python requests api - http multipart post with file and form-data
body: <p>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. </p>
  
  <p>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:</p>
  
  <p>a) Putting everything in the files map</p>
  
  <pre><code>        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")
  </code></pre>
  
  <p>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)</p>
  
  <pre><code>    ## 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")
  </code></pre>
  
  <p>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)</p>
  
  <pre><code>    ## 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.")
  </code></pre>
  
  <p>ValueError: Data must not be a string</p>
  
  <p>Either way the form data is not received by the server because the client encoding fails. Any suggestions?</p>