uday
uday

Reputation: 31

Django request.FILES is empty

I am making a file upload POST API request to django app REST interface. This request is made from another django app view which is receiving the file from the form. I am using poster module

image = request.FILES['image']
from utils.poster.encode import multipart_encode
from utils.poster.streaminghttp import register_openers
register_openers()
datagen, headers = multipart_encode({'file':image.read()})
response = urlfetch.fetch(url="url",
            payload=datagen,
            method=urlfetch.POST,
            headers=headers)

Am I missing any headers?. How django process request with multipart/form-data? This is the error i am getting.

multipart_yielder instance has no attribute '__len__'

Upvotes: 3

Views: 847

Answers (1)

Mariusz Jamro
Mariusz Jamro

Reputation: 31643

GAE's UrlFetch cannot use the output returned by multipart_encode() for payload. UrlFetch.fetch is executing len() on the payload, and the payload returned by multipart_encode is a Python generator, which in general does not support len().

The workaround is to create a payload string first, but it will use lot of memory for large files.

datagen, headers = multipart_encode({'file':image.read()})
data = str().join(datagen)    
response = urlfetch.fetch(url="url",
        payload=data ,
        method=urlfetch.POST,
        headers=headers)

Issue was reported here.

Upvotes: 3

Related Questions