python请求文件上传

时间:2018-12-20 09:47:15

标签: python api request postman

我能够使用邮递员,并使用image和string参数发出请求。如果我从邮递员那里复制python代码并运行它,我将无法执行同样的操作。

import requests

url = "yyyyyyyyyy"

querystring = {"param1":"xxxxx"}

payload = "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"queryFile\"; filename=\"file.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--"
headers = {
    'content-type': "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
    'Authorization': "Bearer yyyyyyyyyyy",
    'cache-control': "no-cache",
    'Postman-Token': "fffffffffff"
    }

response = requests.request("POST", url, data=payload, headers=headers, params=querystring)

print(response.text)

{"message":"EMPTY_FILE_NOT_ALLOWED","status":400}

1 个答案:

答案 0 :(得分:0)

您需要通过files参数传递对您要上传的文件的引用。参见python requests file upload