Search code examples
flaskquery-stringpostmanflask-restful

Retrieve query params with Flask-RESTful


I have a flask-RESTful endpoint defined by:

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>')

I'm testing it manually with Postman. I'd like to pass in null values for start_date and end_date. However:

screenshot of postman interface I've tried modifying the url to:

http://127.0.0.1:5000/events/test/ /  #<--Spaces

and

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

To no avail.


Solution

  • Ended up solving this by using the flask-restful request object, which has an args property which retrieves query params from the request 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')
    

    And then making the requests like: http://127.0.0.1:5000/events?param1=value1&param2=value2 Or to pass null values: http://127.0.0.1:5000/events?param=&param2=value1