首先通过Fiddler或浏览器的开发者工具获取接口地址和请求方法。
当我使用这些数据编写执行接口测试时,卡在了 content-type
中的 boundary
信息与request body部分的 boundary
不匹配的问题上;
即使我调用了 flow.request.set_text
修改request body,但是Fiddler抓取到的信息仍然是未改变的。
解决方案:
- pip 安装 requests_toolbelt。
from requests_toolbelt import MultipartEncoder
。- 创建
MultipartEncoder
对象,内容是字典形式的上传内容。 - 将
header
中content-type
的值指向MultipartEncoder
对象的content-type
属性。 - 发送POST请求。