Python Flask - API Get Endpoint中的非ascii字符

时间:2018-03-07 20:03:36

标签: python python-3.x python-2.7 utf-8

我在Python Flask中构建了一个带有api端点的后端应用程序:/ api / getdata /其中name可以包含瑞典字母,例如Å,Ä和Ö。当我尝试在我的提示中使用curl在API调用中使用这些时,瑞典字符在Python 3.6中就像问号一样。但是当我在Python 2.7中使用它在我的脚本的开头执行此操作时,它可以工作:

import sys
reload(sys)
sys.setdefaultencoding("utf-8")

我该如何解决这个问题?我已经在脚本的开头使用了utf-8注释行。

*更新*

这适用于Python 2,具有上述解决方法,但不适用于Python 3:

app.route('/api/getdata/<name>')
def hello_world(name):
    print(name) # Gives correct in Python 2.7, just a question mark in Python 3
    return name

请求:

curl http://localhost:5000/api/getdata/Åland

0 个答案:

没有答案