Santhosh
Santhosh

Reputation: 451

python request- file upload

I was able to use postman and do a post request with image and a string parameter. I am not able to do the same if I copy the python code from postman and run it.

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}

Upvotes: 0

Views: 150

Answers (1)

Joe Halliwell
Joe Halliwell

Reputation: 1177

You need to pass a reference to the files you want to upload via the files parameter. See python requests file upload.

Upvotes: 1

Related Questions