本文介绍了flask restful:将参数传递给GET请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想通过以下方式创建支持GET请求的资源:
I want to create a resource that supports GET request in following way:
/bar?key1=val1&key2=val2
我尝试了此代码,但无法正常工作
I tried this code, but it is not working
app = Flask(__name__)
api = Api(app)
class BarAPI(Resource):
def get(key1, key2):
return jsonify(dict(data=[key1, key2]))
api.add_resource(BarAPI, '/bar', endpoint='bar')
谢谢!
推荐答案
Flask可以通过请求解析参数
Flask can parse arguments through request
from flask import request
您可以在需要GET参数的块中使用以下行. GET在@app.route()
声明中声明.
You can use following lines in the block that requires GET parameters. GET is declared in @app.route()
declaration.
args = request.args
print (args) # For debugging
no1 = args['key1']
no2 = args['key2']
return jsonify(dict(data=[no1, no2])) # or whatever is required
这篇关于flask restful:将参数传递给GET请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!