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

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

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%                                                                                                              

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:

确定