首先抓取上傳圖片的入參
根據抓取的數據編寫腳本
import requests
from requests_toolbelt import MultipartEncoder
def uploadImg(token, lenderInfoImgType, easyBuyId, agentId,url):
"""
:param token: 請求的token
:param lenderInfoImgType: 上傳圖片的枚舉值
:param easyBuyId: 業務id
:param agentId: 客戶id
:param url: 請求的URL
:return:
"""
headers = {
"agentId": agentId,
"token": token
}
#定義上傳文件的路徑
files = "img/timg.jpg"
#定義請求參數
data = {
"agentId": agentId,
"lenderInfoImgType": lenderInfoImgType,
"easyBuyId": easyBuyId,
"files": ("1.jpg", open(files, "rb"), 'image/jpeg')
}
#將請求參數轉換爲數據流
data = MultipartEncoder(data)
#獲取數據流的Content-Type
headers["Content-Type"] = data.content_type
r = requests.post(url=url, headers=headers, data=data)