title: python requests post file using multipart form parameters
body: <p>I have an API which requires a <b>multipart/form-data content</b> to be send in the body in order to <i>upload a file(image) using POST</i>. I have chosen to use python's requests module as it's easy for newbies like me.There are two form parameters to pass; one is called file, which should be the actual file data and the other is called file-data, which is a dictionary object as below: </p>
  
  <pre><code>    {"param1": param_val,"param2":param_val1}
  </code></pre>
  
  <p>I have seen <a href="http://docs.python-requests.org/en/master/user/quickstart/#post-a-multipart-encoded-file" rel="nofollow noreferrer">http://docs.python-requests.org/en/master/user/quickstart/#post-a-multipart-encoded-file</a> but it doesn't speak about passing form parameters, i.e as if these parameters were values being passed from within a web form which provides placeholders for the file name and the other parameters. Note that if I pass ordinary parameters to the API call like:</p>
  
  <pre><code>    r = requests.post(url, headers= headers,params=params)
  </code></pre>
  
  <p>I would end up sending the parameters after the URL as in <i>URL?param1=param_val>...etc</i> which doesn't work in this case. So I basically need a way to send the form parameters mentioned above as <b>form data of the request</b>. If I could get some guidance around the syntax of the POST call to make in the scenario it would help a lot-</p>
  
  <p><b>****edited for more clarity****</b></p>
  
  <p>The desired HTTP trace of my POST request is as shown below:</p>
  
  <pre><code>    POST &lt;application URL&gt;
      Host: &lt;Host server name&gt;
      Authorization: Basic ABCDXXXX
      Cache-Control: no-cache
      Postman-Token: XXXXXX-ABACC-XXXXX
      Content-Type: multipart/form-data; boundary=----     
  
      ------WebKitFormBoundaryXYXXXXXXXXXXXXXXXXX
      Content-Disposition: form-data; name="file-data"
  
      {"description": "Sample desc","languageCode": "en" }
      ------WebKitFormBoundaryABCXXXXXXXXXXXXX
      Content-Disposition: form-data; name="file"; filename=""
      Content-Type: 
  
  
      ------WebKitFormBoundaryWWWWXXXXXXXXXXXX--
  </code></pre>
  
  <p>If I use the form:</p>
  
  <pre><code>    r = requests.post(url,headers=headers,data=filedata,files=files)
  </code></pre>
  
  <p>I end up getting an error message '400 199-repeated entities[]' which on closer investigation reveals that the request is trying to unpack the tuples in my <b>file-data</b> variable and ends up creating two content boundaries, one for the <b>description</b> and other for the <b>languagecode.</b> Is there a way I can just make it pass file-data in the form parameters just once as shown above?</p>
  
  <p><b>****end of edit*****</b></p>
  
  <p>Thanks in advance-
  Regards </p>