简体   繁体   English

Python - 带有表单数据文件上传的 API POST

[英]Python - API POST with Form-Data file upload

POSTMAN i have tried everything to duplicate a POSTMAN POST that is working using Python and i have been unsuccessful.邮递员我已经尝试了所有方法来复制使用 Python 工作的邮递员 POST,但我没有成功。 I receive a 500 back and when i print the request I dont see anything wrong.我收到了 500 回,当我打印请求时,我没有发现任何问题。 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)

```

Just remove the Content-Type from header.只需从标题中删除Content-Type

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

Explanation here --> How to send a "multipart/form-data" with requests in python?在这里解释 --> 如何在 python 中发送带有请求的“multipart/form-data”?

1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM