Google App Engine self.redirect() POST method

2019-04-09 10:16发布

In GAE (Python), using the webApp Framework, calling self.redirect('some_url') redirects the user to that URL via the GET method. Is it possible to do a (redirect) via the POST method with some parameters as well?

If possible, how?

Thanks!

3条回答
不美不萌又怎样
2楼-- · 2019-04-09 10:31

You can pass parameters. Here is an example:

Let's say you have a main page and you want to POST to '/success'. Usually, you may use this way:

self.redirect('/sucess')

But if you want to pass some parameters from the main page to /success page, like username for example, you can modify the code to this:

self.redirect('/sucess?username=' + username)

In this way, you successfully passed the username value into the URL. In /success page, you can read and store the value by using this:

username = self.request.get('username')

At last, you can make you favorite information onto the /success page by using this simple code:

self.response.out.write('You\'ve succeeded, ' + username + '!')

But, it's of course not a safe way to pass password. I wish it helps.

查看更多
可以哭但决不认输i
3楼-- · 2019-04-09 10:42

This is not possible due to how most clients implement redirection [1]:

However, most existing user agent implementations treat 302 as if it were a 303 response, performing a GET on the Location field-value regardless of the original request method.

So you must use a workaround (like simply calling the method post() from the RequestHandler) or forget the idea.

[1] http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.2

查看更多
相关推荐>>
4楼-- · 2019-04-09 10:46

Looks like there's a similar question asked here: Google App Engine self.redirect post

The answer to that one recommends using the urlfetch.fetch() to do the post.

import urllib

form_fields = {
  "first_name": "Albert",
  "last_name": "Johnson",
  "email_address": "Albert.Johnson@example.com"
}
form_data = urllib.urlencode(form_fields)
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
result = urlfetch.fetch(url=url,
                        payload=form_data,
                        method=urlfetch.POST,
                        headers=headers)
查看更多
登录 后发表回答