https://www.hangge.com/blog/cache/detail_2375.html
https://www.jianshu.com/p/a7bd73de7d34
背景:
项目中有一个接口是需要导入Excel文件,并附带其他参数,过程比较费时,在此记录。
一、抓包使用postman调试
抓包得知Content-Type为multipart/form-data,入参如下图
所以现在postman中调试好接口,配好后如下:
请求结果为:
二、使用python中requests库请求接口
from requests_toolbelt import MultipartEncoder
import requests
import os
url = "http://192.168.2.1/****/uploadfile"
file_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
m = MultipartEncoder(
fields={"isNotKCode": "Y",
"fromCode": "DIST_INTERNAL_022/DIST_INTERNAL_120000/DIST_INTERNAL_120104",
"fromName": "天津/天津市/南开区",
"sendName": "asdfas",
"sendMobile": "18526363636",
"sendPhone": "",
"sendAddress": "e5632452542345",
"uploadType": "1",
"fileType": "3",
"file": (file_path + "/标准模板(不包含发件人信息).xls", open(file_path + "/标准模板(不包含发件人信息).xls", 'rb'),
'application/vnd.ms-excel', {'Expires': '0'})
}
)
response = requests.post(url, data=m, headers={"Content-Type": m.content_type,
"jwt-token": "KYkmsPDBk"})
print(response.json())
注意:单独的requests库已经不满足了,需要使用requests_toolbelt 库,参数格式如代码示例
来源:oschina
链接:https://my.oschina.net/u/4274967/blog/4288477