Note that there are some explanatory texts on larger screens.

plurals
  1. POPost request with multipart/form-data in appengine python not working
    primarykey
    data
    text
    <p>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</p> <pre><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) </code></pre> <p>When this method runs Appengine gives the following warning (I'm not sure if it's related to my issue)</p> <pre><code>Stripped prohibited headers from URLFetch request: ['Content-Length'] </code></pre> <p>And Django sends through the following error</p> <pre><code>&lt;class 'django.utils.datastructures.MultiValueDictKeyError'&gt;"Key 'the_file' not found in &lt;MultiValueDict: {}&gt;" </code></pre> <p>The django code is pretty simple and works when I use the postman chrome extension to send a file.</p> <pre><code>@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()) </code></pre> <p>If I add </p> <pre><code>print request.POST.keys() </code></pre> <p>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.</p> <pre><code>print request.FILES.keys() </code></pre> <p>I get en empty list [].</p> <h1>EDIT 1:</h1> <p>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.</p> <h1>EDIT 2:</h1> <p>I've also tried sending the_file as variations of </p> <pre><code>the_file = file_data.file the_file = file_data.file.read() </code></pre> <p>But I get the same error.</p> <h1>EDIT 3:</h1> <p>I've also tried editing my django app to </p> <pre><code>the_file = request.POST["the_file"] </code></pre> <p>However when I try to save the file locally with</p> <pre><code>path = default_storage.save(file_location, ContentFile(the_file.read())) </code></pre> <p>it fails with </p> <pre><code>&lt;type 'exceptions.AttributeError'&gt;'unicode' object has no attribute 'read'&lt;traceback object at 0x101f10098&gt; </code></pre> <p>similarly if I try access the_file.file (as I can access in my appengine app) it tells me</p> <pre><code>&lt;type 'exceptions.AttributeError'&gt;'unicode' object has no attribute 'file'&lt;traceback object at 0x101f06d40&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload