【发布时间】:2012-04-16 22:04:33
【问题描述】:
我正在尝试修改请求库文档中的 example 以包含从用户上传的文件(到 appengine 应用程序)。我尝试了以下方法:
from libs import requests
file_data = self.request.POST['file_to_upload']
the_file = file_data
send_url = "http://httpbin.org/post"
values = {
'user_id' : '1234',
'file_name' : 'some_file.pdf'
}
r = requests.post(send_url, files=the_file)
logging.info(r.content)
但是这会返回
{
"origin": "81.178.201.22",
"files": {},
"form": {},
"url": "http://httpbin.org/post",
"args": {},
"headers": {
"Content-Length": "0",
"Accept-Encoding": "identity, deflate, compress, gzip",
"Connection": "keep-alive",
"Accept": "*/*",
"User-Agent": "python-requests/0.11.1 AppEngine-Google; (+http://code.google.com/appengine)",
"Host": "httpbin.org",
"Content-Type": ""
},
"json": null,
"data": ""
}
即没有收到文件。我也尝试将 the_file 发送为
file_data.file
file_data.file.read()
但是这些也失败了。最终,我想在同一个发布请求中同时包含“值”和文件,因此类似于:
r = requests.post(send_url, data=values, files=the_file)
但是这也不起作用 - 我想我需要先修复上面的代码。知道我做错了什么吗?
【问题讨论】:
标签: python google-app-engine python-requests