我正在尝试将“#”符号解析为Flask项目中的直接URL。问题是每次请求url时,它都会破坏任何带有#init的值,因为它是url编码中的特殊字符。
localhost:9999/match/keys?source=#123&destination=#123
在烧瓶中,我试图得到这些像这样的参数
app.route(f'/match/keys/source=<string:start>/destination=<string:end>', methods=['GET'])
我在控制台上看到的url响应是这样:
"GET /match/keys/source=' HTTP/1.0" 404 -] happens
我相信您可能不完全了解烧瓶中的“查询字符串”是如何工作的。这个网址:
app.route(f'/match/keys/source=<string:start>/destination=<string:end>', methods=['GET'])
将无法满足您的期望,因为它与请求不符:
localhost:9999/match/keys?source=#123&destination=#123
宁可成为:
@app.route('/match/keys', methods=['GET'])
这将匹配:
localhost:9999/match/keys?source=%23123&destination=%23123
然后捕获您执行的那些“查询字符串”:
source = request.args.get('source') # <- name the variable what you may
destination = request.args.get('destination') # <- same as the naming format above
因此,当您调用localhost:9999/match/keys?source=%23123&destination=%23123
时,将在请求url中测试那些“查询字符串”,如果是,则将执行路由功能。
我编写了此测试:
def test_query_string(self):
with app.test_client() as c:
rc = c.get('/match/keys?source=%23123')
print('Status code: {}'.format(rc.status_code))
assert rc.status_code == 200
assert 'source' in request.args
它使用此路由功能通过:
@app.route('/match/keys', methods=['GET'])
def some_route():
s = request.args.get('source')
return s