开发者

Google App Engine self.redirect() POST method

In GAE (Python), using the webApp Framework, calling self.redirect('some_url') redirects the user to that URL via the GET method. I开发者_运维百科s it possible to do a (redirect) via the POST method with some parameters as well?

If possible, how?

Thanks!


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


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.


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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜