I'm trying to programmatically POST rather than using a web form. To do this, I'm using the MultipartPostHander module downloaded from https://pypi.python.org/pypi/MultipartPostHandler/.

It seems to work well (ie. does the same thing that a web form POST request would do), except users.get_current_user() returns None in the POST page handler. In comparison, the same POST page handler returns the user info correctly, if the POST request is submitted by a web form.

Also tried with poster (http://atlee.ca/software/poster/index.html) and had the exact same issue.

Why could this be happening? Thank you in advance.

class first_page_handler(basehandler.BaseHandler):
     def get(self):
         user = users.get_current_user()
         if user:
            raw_upload_url = webapp2.uri_for('test_module')
            qimage_upload_url = r'http://localhost:8080'+raw_upload_url
            params = {'param1': 'param'}

            opener = urllib2.build_opener(multipost.MultipartPostHandler)
            urllib2.install_opener(opener)
            req = urllib2.Request(qimage_upload_url, params)

            response = urllib2.urlopen(req)   #  <<<< POST request submitted
            text_response = response.read().strip()
            return self.response.write("<html><body><p>"+text_response+"</p></body>
                                       </html>")
         else:
            login_url = users.create_login_url("/admin")
            context = {'login_url': login_url}
            self.render_response('admin/login.html', **context)

class post_page_hander(basehandler.BaseHandler):
    def post(self):
       user = users.get_current_user()    #  << Returns NONE
       return self.response.write("<html><body><p>"+user.email()+"</p></body></html>")
有帮助吗?

解决方案

You're not setting any cookies on the request, so the server has no idea what the current user is. (What you're doing is unusual ... there is probably a better way of doing whatever it is you want to do.)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top