python 上传图片并且传递参数

import random
import string
from requests_toolbelt import MultipartEncoder
import requests
import os.path
# 鉴权
Authorization ='Bearer '+"xxx"
url = "xxx"  # 请求地址
#图片存在的目录
img_folder =r'xxx' 
for img_name in os.listdir(img_folder):
    #需要上传文件的url
    src = os.path.join(img_folder, img_name)
    print(src)
    # 请求体
    fields = {
        'fileList': (
            img_name, open(src, "rb"),
            "image/jpeg"),
        "ocrType": 'IT_TEST',
    }
    #data = {'width': '1920', 'height': '1080','method':1}
    # 因为16位数随机的,每次都不一样
    #boundary = '----WebKitFormBoundary' \
    #           + ''.join(random.sample(string.ascii_letters + string.digits, 16))
    #m = MultipartEncoder(fields=fields, boundary=boundary)
    #headers_data = {"Content-Type": m.content_type, "Authorization": Authorization}
    #req = requests.post(url=url, headers=headers_data, data=m)
    #req = requests.post(url=url, headers=headers_data,files=fields,data=data)
    #print(req.json())
    #header中传鉴权需要的Authorization,从登录接口中拿过来的
    headers_data = {"Authorization": Authorization}
    files = {'file': open(src, 'rb')}
    data = {'width': '1920', 'height': '1920','method':1}
    response = requests.post(url, files=files, data=data,headers=headers_data)
    json = response.json()
    print(json)

你可能感兴趣的:(python,java,开发语言)