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?