How to send a “multipart/form-data” with requests in python?

前端 未结 9 1788
野趣味
野趣味 2020-11-22 01:29

How to send a multipart/form-data with requests in python? How to send a file, I understand, but how to send the form data by this method can not understand.

9条回答
  •  爱一瞬间的悲伤
    2020-11-22 02:00

    Here is the simple code snippet to upload a single file with additional parameters using requests:

    url = 'https://'
    fp = '/Users/jainik/Desktop/data.csv'
    
    files = {'file': open(fp, 'rb')}
    payload = {'file_id': '1234'}
    
    response = requests.put(url, files=files, data=payload, verify=False)
    

    Please note that you don't need to explicitly specify any content type.

    NOTE: Wanted to comment on one of the above answers but could not because of low reputation so drafted a new response here.

提交回复
热议问题