【问题标题】:Trouble sending an uploaded file using the Python requests library使用 Python 请求库发送上传文件时遇到问题
【发布时间】: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


    【解决方案1】:

    GAE 不支持请求库。 看看这个:Using the Requests python library in Google App Engine

    【讨论】:

    【解决方案2】:

    您能否提供一个用于显示上传表单的 HTML 代码? 有时,表单上传失败,因为它缺少一些 HTML 属性。

    【讨论】:

    • 嗨,Takashi,问题不在于客户端——我实际上是在修改一种工作方法,将上传的文件发送到以前我用电子邮件作为附件发送的 api。客户端没有任何改变,如果我输入 print type(the_file.file) 我会根据需要得到 stringIO。
    猜你喜欢
    • 2014-11-02
    • 2020-06-20
    • 1970-01-01
    • 1970-01-01
    • 2018-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多