如何将提取的 python 请求 URL 变量转换为字符串?

How to convert extracted python request URL variable to string?

我是 python 网络应用程序的新手。我正在为我的 Web 应用程序使用 Bottlepy,并请求从 URLs 中提取变量。我提取了变量first=request.query.first。我可以 return 变量的值但是当我想在 if 条件下使用它时它总是失败并且执行 else 块。

URL: http://127.0.0.1:8080/h?first="hi"

@app.route('/h')
def test():
    first=request.query.first
    if first == "hi":
        return "passed"
    else:
        return "fail"

感谢您对问题的帮助,谢谢。

这应该有效地将 URL 变量转换为字符串。 在你做了 first=request.query.first

之后
newfirst=str(first)

然后在测试函数中把first改成newfirst

def test():
    if newfirst == "hi":
        return "passed"
    else:
        return "fail"

我测试了 URL 很多次然后我注意到我在 "hi" 周围加上了引号删除引号解决了问题。新的 URL 是 http://localhost:8080/h?first=hi.