英文:
Python Flask not parsing GET arguments passed to route
问题
以下是你的代码的中文翻译:
from flask import request
@app.route("/pods", methods=["GET"])
def list_pods():
cluster = request.args.get("cluster")
project = request.args.get("project_name")
print(cluster)
print(project)
以下是你的问题的中文翻译:
如何让Flask解析两个GET参数?
英文:
Here is my simple code:
from Flask import request
@app.route("/pods", methods=["GET"])
def list_pods():
cluster = request.args.get("cluster")
project = requests.args.get("project_name")
print(cluster)
print(project)
Running the following two cURL commands results in the second-defined variable to not get parsed:
$ curl localhost:51918/pods?cluster=nw-dc-blah&project_name=my_project
nw-dc-blah
None
$ curl localhost:51918/pods?project_name=my_project&cluster=nw-dc-blah
None
my_project
How can I get Flask to parse both GET arguments?
答案1
得分: 1
如https://stackoverflow.com/questions/56564922/flask-app-query-parameters-dropping-out-of-request-args中所述
cURL URL需要用引号括起来以转义&
符号,因为我的Unix shell在那之后不解析任何内容并将其发送到Flask。
$ curl "localhost:51918/pods?cluster=nw-dc-blah&project_name=my_project
nw-dc-blah"
英文:
As stated in https://stackoverflow.com/questions/56564922/flask-app-query-parameters-dropping-out-of-request-args
The cURL URL will need to have quotes around it to escape the &
sign, as my Unix shell doesn't parse anything after that and send it to Flask.
$ curl "localhost:51918/pods?cluster=nw-dc-blah&project_name=my_project
nw-dc-blah"
答案2
得分: 1
To reproduce your example:
hello.py
from flask import Flask,request
app = Flask(__name__)
@app.route("/pods", methods=["GET"])
def list_pods():
cluster = request.args.get("cluster")
project = request.args.get("project_name")
print(cluster)
print(project)
return "hello"
Enclosing characters in double quotes ("
) preserves the literal value of all characters within the quotes
USE ("
) else following:
➜ stackoverflow curl localhost:5000/pods?cluster=nw-dc-blah&project_name=my_project
[1] 96485
zsh: no matches found: localhost:5000/pods?cluster=nw-dc-blah
[1] + 96485 exit 1 curl localhost:5000/pods?cluster=nw-dc-blah
➜ stackoverflow curl "localhost:5000/pods?cluster=nw-dc-blah&project_name=my_project"
hello%
英文:
To reproduce your example:
hello.py
from flask import Flask,request
app = Flask(__name__)
@app.route("/pods", methods=["GET"])
def list_pods():
cluster = request.args.get("cluster")
project = request.args.get("project_name")
print(cluster)
print(project)
return "hello"
Then
flask --app hello run
Enclosing characters in double quotes (‘"
’) preserves the literal value of all characters within the quotes
USE ("
) else following:
➜ stackoverflow curl localhost:5000/pods?cluster=nw-dc-blah&project_name=my_project
[1] 96485
zsh: no matches found: localhost:5000/pods?cluster=nw-dc-blah
[1] + 96485 exit 1 curl localhost:5000/pods?cluster=nw-dc-blah
➜ stackoverflow curl "localhost:5000/pods?cluster=nw-dc-blah&project_name=my_project"
hello%
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论