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

前端 未结 9 1909
野趣味
野趣味 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 01:58

    Here is the python snippet you need to upload one large single file as multipart formdata. With NodeJs Multer middleware running on the server side.

    import requests
    latest_file = 'path/to/file'
    url = "http://httpbin.org/apiToUpload"
    files = {'fieldName': open(latest_file, 'rb')}
    r = requests.put(url, files=files)
    

    For the server side please check the multer documentation at: https://github.com/expressjs/multer here the field single('fieldName') is used to accept one single file, as in:

    var upload = multer().single('fieldName');
    

提交回复
热议问题