You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python Flask路由无法解析多个GET参数问题求助

Flask GET参数解析问题的解决方案

问题根源及修复步骤

1. 代码中的拼写错误

你的代码存在拼写错误:获取project_name参数时误用了requests.args.get(),但实际导入的是Flask的request对象(并非requests第三方库),正确写法应为request.args.get()。另外注意Flask库的导入需用小写flask。

修正后的代码:

from flask import request

@app.route("/pods", methods=["GET"])
def list_pods():
    cluster = request.args.get("cluster")
    project = request.args.get("project_name")  # 将requests改为request
    
    print(cluster)
    print(project)

2. curl命令的Shell转义问题

你执行curl时,&在Bash等Shell环境中是特殊字符,会把前面的命令放到后台运行,导致第二个参数被Shell截断,无法传递给Flask服务。

解决方法是用引号(单引号或双引号)包裹整个URL:

$ curl 'localhost:51918/pods?cluster=nw-dc-blah&project_name=my_project'

或

$ curl "localhost:51918/pods?project_name=my_project&cluster=nw-dc-blah"

完成以上两处修正后,Flask即可正确解析两个GET参数。

内容的提问来源于stack exchange,提问作者Christopher Sinclair

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 11:40:08