requests上傳圖片

首先抓取上傳圖片的入參

根據抓取的數據編寫腳本

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)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章