ksusha
ksusha

Reputation: 15

How to route non-ascii URLs in Flask python

Good afternoon, everyone!

I have a problem with the routing my URL adress to Flask, precisely with running it in web-browser. All I want is to transfer the sharp symbol "#" and some Russian words (as like "#привет" or "#ПомогитеМнеПожалуйста") together.

The screenshot of error: enter image description here

My programming code at the moment looks like this:

# -*- coding: utf-8 -*-
from flask import Flask, jsonify
app = Flask(__name__)

@app.route('/hashtags/' + b'<names>'.decode('utf-8'), methods=['GET'])
def get_hashtags(names):
    return jsonify({'Segmentation Hashtags': names})


if __name__ == '__main__':
    app.run(port=9876)

So, basically, <names> is a parameter from function get_hashtag that is used for transfering my future hashtag to the web-browser using jsonify. I need to find the way of transfering any hashtag I want with sharp symbol "#" plus Russian letters. As far as I know, there is an ASCII-coding methods, but I have no idea how to use it properly. And <names> have structure: "#привет"

Thanks in advance!

Upvotes: 1

Views: 2831

Answers (2)

Eric Stdlib
Eric Stdlib

Reputation: 1562

You have to use %23 instead of #, because the hash symbol marks a fragment in a URL. Wikipedia

So the actual URL app.route is getting is /hashtag/

It seems to be impossible to get the content after #. See this.

Upvotes: 1

Iron Fist
Iron Fist

Reputation: 10951

Try to decode your url before passing it to your view methods, this way:

@app.route('/hashtags/<names>'.encode('utf-8'), methods=['GET'])

Upvotes: 0

Related Questions