Python Flask未解析传递给路由的GET参数。

huangapple go评论90阅读模式
英文:

Python Flask not parsing GET arguments passed to route

问题

以下是你的代码的中文翻译:

  1. from flask import request
  2. @app.route("/pods", methods=["GET"])
  3. def list_pods():
  4. cluster = request.args.get("cluster")
  5. project = request.args.get("project_name")
  6. print(cluster)
  7. print(project)

以下是你的问题的中文翻译:

如何让Flask解析两个GET参数?

英文:

Here is my simple code:

  1. from Flask import request
  2. @app.route("/pods", methods=["GET"])
  3. def list_pods():
  4. cluster = request.args.get("cluster")
  5. project = requests.args.get("project_name")
  6. print(cluster)
  7. print(project)

Running the following two cURL commands results in the second-defined variable to not get parsed:

  1. $ curl localhost:51918/pods?cluster=nw-dc-blah&project_name=my_project
  2. nw-dc-blah
  3. None
  1. $ curl localhost:51918/pods?project_name=my_project&cluster=nw-dc-blah
  2. None
  3. 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。

  1. $ curl "localhost:51918/pods?cluster=nw-dc-blah&project_name=my_project
  2. 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.

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

答案2

得分: 1

To reproduce your example:

  1. hello.py
  2. from flask import Flask,request
  3. app = Flask(__name__)
  4. @app.route("/pods", methods=["GET"])
  5. def list_pods():
  6. cluster = request.args.get("cluster")
  7. project = request.args.get("project_name")
  8. print(cluster)
  9. print(project)
  10. return "hello"

Enclosing characters in double quotes (") preserves the literal value of all characters within the quotes

USE (") else following:

  1. stackoverflow curl localhost:5000/pods?cluster=nw-dc-blah&project_name=my_project
  2. [1] 96485
  3. zsh: no matches found: localhost:5000/pods?cluster=nw-dc-blah
  4. [1] + 96485 exit 1 curl localhost:5000/pods?cluster=nw-dc-blah
  5. stackoverflow curl "localhost:5000/pods?cluster=nw-dc-blah&project_name=my_project"
  6. hello%
英文:

To reproduce your example:

  1. hello.py
  2. from flask import Flask,request
  3. app = Flask(__name__)
  4. @app.route("/pods", methods=["GET"])
  5. def list_pods():
  6. cluster = request.args.get("cluster")
  7. project = request.args.get("project_name")
  8. print(cluster)
  9. print(project)
  10. return "hello"
  11. Then
  12. flask --app hello run

Enclosing characters in double quotes (‘"’) preserves the literal value of all characters within the quotes

USE (") else following:

  1. stackoverflow curl localhost:5000/pods?cluster=nw-dc-blah&project_name=my_project
  2. [1] 96485
  3. zsh: no matches found: localhost:5000/pods?cluster=nw-dc-blah
  4. [1] + 96485 exit 1 curl localhost:5000/pods?cluster=nw-dc-blah
  5. stackoverflow curl "localhost:5000/pods?cluster=nw-dc-blah&project_name=my_project"
  6. hello%

huangapple
  • 本文由 发表于 2023年4月19日 23:07:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76056093.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定