踩坑接口请求参数含文件
- requests
- 接口请求既有file,也有json。划重点params
requests
官网地址
https://requests.readthedocs.io/en/stable/user/quickstart/#post-a-multipart-encoded-file
接口请求既有file,也有json。划重点params
import requestsurl = "https://xx.xxxxx.com/api"# json类型的请求参数
payload = {'type': '123','doctype': '1'}
# 文件参数,注意files一定是列表啊,官网写的字典不行!!!
# 比较优雅的写的话,file最好用with open啊,自动colse。
# with open('xxx/xxx.docx', 'rb') as f:
# files = [
# ('file', ('xxx.docx',
# f,
# 'application/vnd.openxmlformats-#officedocument.wordprocessingml.document'))
# ]
files=[('file',('xxx.docx',open('/xxx/xxx/xxx/xxx.docx','rb'),'application/vnd.openxmlformats-officedocument.wordprocessingml.document'))
]
# 请求头类型-文件
headers = {'Content-Type': 'multipart/form-data',}# 重点是,用params传json。如果用data就不行
response = requests.request("POST", url, headers=headers, params=payload, files=files)print(response.text)