1

I'm trying to modify the example in the requests library documentation to include a file uploaded from a user (to an appengine app). 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": ""
}

i.e. there is no file received. I've also tried sending the_file as

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?

Jeremy
  • 1
  • 85
  • 340
  • 366
user714852
  • 2,054
  • 4
  • 30
  • 52

2 Answers2

0

Could you provide an HTML code for displaying the upload form? Sometime, form upload fails because it lacks some HTML attributes.

Takashi Matsuo
  • 3,406
  • 16
  • 25
  • Hi Takashi, the problem isn't client side - I'm essentially modifying a working method to send an uploaded file to an api where previously I used to email it as an attachment. Nothing has changed client side and if I type in print type(the_file.file) I get stringIO as required. – user714852 Apr 17 '12 at 15:17
0

the requests library is not supported on GAE. check this out: Using the Requests python library in Google App Engine

Community
  • 1
  • 1
aschmid00
  • 7,038
  • 2
  • 47
  • 66
  • Thanks for the heads up on this. It seems like this was a problem with urllib3, however according to this (http://pypi.python.org/pypi/urllib3/#changes) it is now supported, and according to this http://stackoverflow.com/a/9763217/714852 something should change to allow requests on GAE soon. – user714852 Apr 17 '12 at 15:15