I'm attempting to send a multipart post request from an appengine app to an external (django) api hosted on dotcloud. The request includes some text and a file (pdf) and is sent using the following code
from google.appengine.api import urlfetch
from poster.encode import multipart_encode
from libs.poster.streaminghttp import register_openers
register_openers()
file_data = self.request.POST['file_to_upload']
the_file = file_data
send_url = "http://127.0.0.1:8000/"
values = {
'user_id' : '12341234',
'the_file' : the_file
}
data, headers = multipart_encode(values)
headers['User-Agent'] = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
data = str().join(data)
result = urlfetch.fetch(url=send_url, payload=data, method=urlfetch.POST, headers=headers)
logging.info(result.content)
When this method runs Appengine gives the following warning (I'm not sure if it's related to my issue)
Stripped prohibited headers from URLFetch request: ['Content-Length']
And Django sends through the following error
<class 'django.utils.datastructures.MultiValueDictKeyError'>"Key 'the_file' not found in <MultiValueDict: {}>"
The django code is pretty simple and works when I use the postman chrome extension to send a file.
@csrf_exempt
def index(request):
try:
user_id = request.POST["user_id"]
the_file = request.FILES["the_file"]
return HttpResponse("OK")
except:
return HttpResponse(sys.exc_info())
If I add
print request.POST.keys()
I get a dictionary containing user_id and the_file indicating that the file is not being sent as a file. if I do the same for FILES i.e.
print request.FILES.keys()
I get en empty list [].
EDIT 1:
I've changed my question to implement the suggestion of someone1 however this still fails. I also included the headers addition recommended by the link Glenn sent, but no joy.
EDIT 2:
I've also tried sending the_file as variations of
the_file = file_data.file
the_file = file_data.file.read()
But I get the same error.
EDIT 3:
I've also tried editing my django app to
the_file = request.POST["the_file"]
However when I try to save the file locally with
path = default_storage.save(file_location, ContentFile(the_file.read()))
it fails with
<type 'exceptions.AttributeError'>'unicode' object has no attribute 'read'<traceback object at 0x101f10098>
similarly if I try access the_file.file (as I can access in my appengine app) it tells me
<type 'exceptions.AttributeError'>'unicode' object has no attribute 'file'<traceback object at 0x101f06d40>
See Question&Answers more detail:
os