Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7793379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:28:11+00:00 2026-06-01T22:28:11+00:00

I’m attempting to send a multipart post request from an appengine app to an

  • 0

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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T22:28:14+00:00Added an answer on June 1, 2026 at 10:28 pm

    Here is some code I tested locally that should do the trick (I used a different handler than webapp2 but tried to modify it to webapp2. You’ll also need the poster lib found here http://atlee.ca/software/poster/):

    In your POST handler on GAE:

    from google.appengine.api import urlfetch
    from poster.encode import multipart_encode
    payload = {}
    payload['test_file'] = self.request.POST['test_file']
    payload['user_id'] = self.request.POST['user_id']
    to_post = multipart_encode(payload)
    send_url = "http://127.0.0.1:8000/"
    result = urlfetch.fetch(url=send_url, payload="".join(to_post[0]), method=urlfetch.POST, headers=to_post[1])
    logging.info(result.content)
    

    Make sure your HTML form contains method="POST" enctype="multipart/form-data". Hope this helps!

    EDIT:
    I tried using the webapp2 handler and realized the way files are served are different than how the framework I used to test with works (KAY). Here is updated code that should do the trick (tested on production):

    import webapp2
    from google.appengine.api import urlfetch
    from poster.encode import multipart_encode, MultipartParam
    
    class UploadTest(webapp2.RequestHandler):
      def post(self): 
        payload = {}
        file_data = self.request.POST['test_file']
        payload['test_file'] = MultipartParam('test_file', filename=file_data.filename,
                                              filetype=file_data.type,
                                              fileobj=file_data.file)
        payload['name'] = self.request.POST['name']
        data,headers= multipart_encode(payload)
        send_url = "http://127.0.0.1:8000/"
        t = urlfetch.fetch(url=send_url, payload="".join(data), method=urlfetch.POST, headers=headers)
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.out.write(t.content)
      def get(self):
        self.response.out.write("""
        <html>
            <head>
                <title>File Upload Test</title>
            </head>
            <body>
                <form action="" method="POST" enctype="multipart/form-data">
                    <input type="text" name="name" />
                    <input type="file" name="test_file" />
                    <input type="submit" value="Submit" />
                </form>
            </body>
        </html>""")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am currently running into a problem where an element is coming back from
I am writing an app with both english and french support. The app requests

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.