0

POSTMAN i have tried everything to duplicate a POSTMAN POST that is working using Python and i have been unsuccessful. I receive a 500 back and when i print the request I dont see anything wrong. Any suggestions?

```
filename1="untitled.png"
filename="untitled.png"
location = "C:\\myfiles\\"


#files = {"file":(filename1,open(location+'/'+filename,"rb"),'application-type')}
#files = {"file":(filename1,open(location+'/'+filename,"rb"))}
#files = {"file":(filename1, open(location+'/'+filename,'rb'),'application-type')}
files = {'file': (filename1, open(location+'/'+filename,'rb'))}


payload = {'': {
    "DateIssued": "2020-02-22T14:35:40.760026-08:00",
    "Category":None,
    "Title":"Report",
    "Title2":"MyReports"
}}

headers = {
 'Content-Type': 'application/x-www-form-urlencoded',
  'PublishService-Api-Key': 'myAPIKey'

}

req = requests.request('POST',"http://myurl.com/PublishService/api/docs/", headers=headers, data=payload, files=files)
print(req)

```

1 Answers1

0

Just remove the Content-Type from header.

headers = {
  'PublishService-Api-Key': 'myAPIKey'
}

Explanation here --> How to send a "multipart/form-data" with requests in python?

1

gsb22
  • 2,112
  • 2
  • 10
  • 25
  • After removing Content Type it i still receive a 500 response – user6891307 Mar 11 '20 at 19:09
  • try this, with modified header . `req = requests.post("http://myurl.com/PublishService/api/docs/", headers=headers, data=payload, files=files)` – gsb22 Mar 11 '20 at 19:11
  • can you also check the error message, status code is 500 but because of what? – gsb22 Mar 11 '20 at 19:12
  • your postman snip shows, you are passing the image under `myfile` variable whereas your python code has `file` key. `files = {'file': (filename1, open(location+'/'+filename,'rb'))}` Change it to myfile – gsb22 Mar 11 '20 at 19:19
  • Thank you for the response. Our API uses the key as the name of the file in the system. i can change that to anything and it will give me a 200 in POSTMAN and be received correctly. Just as long as a valid file is attached. If it is not i receive a 500. i am not sure how to check the errors. i just see.{"Message":"An error has occurred."} – user6891307 Mar 11 '20 at 20:00