pythonpostpython-requestspostman

Python script generated by Postman not working


The below code is basically generated by Postman. I have removed 'Content-Type' from the header as I understand that is not needed. I've also generalised the URL path etc. However I receive an error message: b'{"Message":"An error has occured. Details: Error writing MIME multipart body part to output stream."}'

The request however does work when I run it from Postman. What am I doing wrong?

import requests

url = "https://website.com/api/inputs/file/upload/Test/12/"

payload = {}
files = [
  ('', open('C:/Users/jmas/Documents/Demo/test.csv','rb'))
]
headers = {
  'Authorization': 'Bearer 0eE6r7DWAIriN6gBR1-5WJI- 
9ZsfgE13JEthzuSXKQ9A05sbt5xdn8cAcV3Sz16D4thYNBcr6dQGdIcivSOpo- 
0dz7tAVP19trL2bwQtQez6FyzZqJFqPQrHm7fLee9eEr5GSpth0JfeqV5Gt7z_juqB3dHDBINu1oxh9G- 
pF8VuSRpUkAOujSMS0RysE1aptVqU1wAXLTXnKxUlDJLpTuQMCQGnFwqNvUWx3mDQ9xh4pw-ZaKw8TMvWaYgtmd1Z- 
oAp2IgvP9bwV5pv5izyuUWIfaZyP0mqYlZu2'
}

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

print(response.text.encode('utf8'))
print(response.status_code)

Solution

  • Success! I added 'text/csv' to the files parameter (dictionary) and the file successfully uploaded.

    files = {"file": ("PremSet_2UB_0LB_NoRenewalInfo", 
    open('C:/Users/jmas/Documents/Demo/test.csv', 'rb'), 'text/csv')}