flask - 使用Flask-RESTful检索查询参数

标签 flask query-string postman flask-restful

我有一个flask-RESTful端点,定义为:

class SearchEvents(Resource):
    def get(self, name, start_date, end_date):
         #do stuff

api.add_resource(endpoints.SearchEvents, '/events/<string:name>/<string:start_date>/<string:end_date>')

我正在用 postman 手动测试它。我想传递start_date和end_date的空值。然而:

screenshot of postman interface
我尝试将网址修改为:
http://127.0.0.1:5000/events/test/ /  #<--Spaces


http://127.0.0.1:5000/events/test/""/""

无济于事。

最佳答案

最终通过使用flask-restful request对象解决了这个问题,该对象具有args属性,该属性从请求url中检索查询参数:

from flask_restful import request #import request from flask restful

class SearchEvents(Resource):
    def get(self):
        args = request.args #retrieve args from query string

api.add_resource(endpoints.SearchEvents, '/events')

然后发出如下请求:http://127.0.0.1:5000/events?param1=value1&param2=value2或传递空值:http://127.0.0.1:5000/events?param=&param2=value1

关于flask - 使用Flask-RESTful检索查询参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50882956/

相关文章:

python - 类型错误 : Object of type X is not JSON serializable

python - 我在我的 flask 中遗漏了什么 restful api 输出是空的并且重复了

asp.net-mvc - ASP.NET MVC - 重写 FormMethod.Get 查询字符串?

javascript - 带感叹号的 encodeUriComponent?

ruby-on-rails - 在设计注册中保留查询字符串

python - 如何使用 Flask 缓存变量?

python - 向应用程序工厂注册 flask 管理 View 的正确方法

c# - 如何在 url 中将字符串作为参数传递给 asp.net core api Controller

javascript - 如何在 postman 中使用 javascript 将唯一对象添加到 json 数组

java - Rest url 中查询参数的日期时间格式