【发布时间】:2020-11-20 16:41:47
【问题描述】:
在本地运行 Django 应用程序,我可以使用 Postman 上传 zip 文件以及一些 dict 数据。在 'def Post()' 中破坏应用程序,我可以看到 Postman 上传成功:
request.data = <QueryDict: {'request_id': ['44'], 'status': [' Ready For Review'], 'is_analyzed': ['True'], 'project': ['test value'], 'plate': ['Plate_R0'], 'antigen': ['tuna'], 'experiment_type': ['test'], 'raw_file': [<TemporaryUploadedFile: testfile.zip (application/zip)>]}>
Postman 提供以下 python 代码来在我的 python 脚本中复制这些结果:
import requests
url = "http://127.0.0.1:8000/api/upload/"
payload = {'request_id': '44',
'status': ' Ready For Review',
'is_analyzed': 'True',
'project': 'test value',
'plate': 'Plate_R0',
'antigen': 'tuna',
'experiment_type': 'test'}
files = [
('raw_file', open(r'C:/testfile.zip','rb'))
]
headers = {
'Content-Type': 'application/x-www-form-urlencoded'
}
response = requests.request("POST", url, headers=headers, data = payload, files = files)
print(response.text.encode('utf8'))
直接运行此代码并检索 request.data(服务器端)我看到 xlsx 文件的二进制表示在对象中,并且有效负载数据不存在(这是响应中的错误)。
如何让我的 python 脚本生成与邮递员相同的服务器端对象?具体来说,我如何上传我的数据,使文件表示为:
谢谢。
【问题讨论】:
标签: python-3.x django django-rest-framework upload postman