简体   繁体   English

使用Python请求库发送上传的文件时出现问题

[英]Trouble sending an uploaded file using the Python requests library

I'm trying to modify the example in the requests library documentation to include a file uploaded from a user (to an appengine app). 我正在尝试修改请求库文档中的示例 ,以包括从用户上传到appengine应用程序的文件。 I have tried the following: 我尝试了以下方法:

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)

However this returns 但是,这返回

 {
  "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": ""
}

ie there is no file received. 即没有收到文件。 I've also tried sending the_file as 我也尝试过将the_file发送为

file_data.file 
file_data.file.read() 

however these also fail. 但是这些也失败了。 Ultimately I want to include both teh "values" and the file in the same post request, so something like: 最终,我想在同一发布请求中同时包含“值”和文件,因此类似:

r = requests.post(send_url, data=values, files=the_file)

However this isn't working either - I guess I need to fix the above code first. 但是,这也不起作用-我想我需要首先修复上面的代码。 Any idea as to what I'm doing wrong? 关于我在做什么错的任何想法吗?

Could you provide an HTML code for displaying the upload form? 您能否提供用于显示上传表单的HTML代码? Sometime, form upload fails because it lacks some HTML attributes. 有时,表单上载失败,因为它缺少一些HTML属性。

the requests library is not supported on GAE. GAE不支持请求库。 check this out: Using the Requests python library in Google App Engine 检查一下: 在Google App Engine中使用Requests python库

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

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