Python3发送multipart/form-data编码
在接口自动化测试中,我们经常需要使用Python3和requests库来上传文件,发送multipart/form-data编码。而requests_toolbelt是一个很好用的库,可以帮助我们更方便地实现这个功能。在本篇文章中,我们将为大家介绍如何使用requests_toolbelt库来上传文件,发送multipart/form-data编码。
01 安装依赖
首先,我们需要安装requests_toolbelt库。在命令行中输入以下命令即可:
pip install requests_toolbelt
02 上传单个文件的例子
from requests_toolbelt.multipart.encoder import MultipartEncoder
import requests
url = "http://httpbin.org/post"
encoder = MultipartEncoder(
fields={
'file1': ('test.txt',