说明:
该方式适用于请求类型 multipart/form-data。
具体表现是请求头(header)中
  1. Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryaPJUIAvGfcSPVJIJ
1、安装requests-toolbelt

  1. pip install requests-toolbelt
2、具体使用如下:

  1. from requests_toolbelt import MultipartEncoder
  2. import requests
  # text/plain 需要根据实际情况替换,可能是:'image/png'
  # fields 里参数个数是根据实际来定义的,几个文件就定义几个field ,具体的名字要根据实际情况调整

  1. m = MultipartEncoder(
  2.     fields={'field0': 'value', 'field1': 'value',
  3.             'field2': ('filename', open('file.py', 'rb'), 'text/plain')}
  4.     )

  5. r = requests.post('http://httpbin.org/post', data=m,
  6.                   headers={'Content-Type': m.content_type})
  7. # 当然 header 可以在自己定义好的header里加入'Content-Type': m.content_type


10-04 17:17
查看更多