Retrieving the url anchor in a werkzeug request

我们两清 提交于 2019-11-28 11:12:14

From Wikipedia (Fragment Identifier) (don't have the time to find it in the RFC):

The fragment identifier functions differently than the rest of the URI: namely, its processing is exclusively client-side with no participation from the server

So Flask - or any other framework - doesn't have access to #ghi.

I ran into the same problem. Facebook authentication API returns the access token behind a hash appended into the redirection url. In the same way, Flask's request.url drops everything in the URL behind the hash sign.

I'm also using Flask so I think you can use my brute-force workaround using Javascript's window.location.href to get the full URL. Then, I just extracted the piece that I needed (the access token), put it into a redirection URL where I can pass the access token as an argument to the receiving view function. Here's the code:

@app.route('/app_response/<response>', methods=['GET'])
def app_response_code(response):
    return '''  <script type="text/javascript">
                var token = window.location.href.split("access_token=")[1]; 
                window.location = "/app_response_token/" + token;
            </script> '''

@app.route('/app_response_token/<token>/', methods=['GET'])
def app_response_token(token):
    return token

In case you manage(d) to do this within Werkzeug, I'm interested to know how.

tamarabyte

You can do this using flask.url_for with the _anchor keyword argument:

url_for('abc.def', _anchor='ghi')
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!