记一段python代码,requests调用formdata上传照片
from urllib3 import encode_multipart_formdata #主要是引用这个传为数据流
from config import readConfig
class PhotoSubmit:
def photo_submit(self, token, photo_type):
# 获取存放图片的文件夹绝对路径
docs_path = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__)))), 'docs')
files = {'upload': ('image.png', open(os.path.join(docs_path, '400k.png'), 'rb').read(), 'multipart/form-data'),
'type': photo_type}
encode_data = encode_multipart_formdata(files)
url = 'xxxxxxxxxxxxxxxxxxxxxxx'
headers = {'content-type': encode_data[1], 'token': token}
data = encode_data[0]
result = requests.post(url=url, headers=headers, data=data).json()
return result
参考https://blog.csdn.net/u013511989/article/details/80422734