Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 801
Toby Weed Avatar asked Jun 15 '18 21:06

Toby Weed


People also ask

How do I get query params in Flask?

To access an individual known param passed in the query string, you can use request. args. get('param') . This is the "right" way to do it, as far as I know.

CAN REST API have query parameters?

You can use query parameters to control what data is returned in endpoint responses. The sections below describe query parameters that you can use to control the set of items and properties in responses, and the order of the items returned.

Is flask-RESTful deprecated?

The whole request parser part of Flask-RESTful is slated for removal and will be replaced by documentation on how to integrate with other packages that do the input/output stuff better (such as marshmallow). This means that it will be maintained until 2.0 but consider it deprecated.

How to get get parameters from a flask Request object?

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 While Flask request object make it easy to retrieve GET parameters, it's not doing any of the data validation.

How to get the key from a query in flask?

Now in order to retrieve the key from query, we have 2 ways, with both of them having some subtle differences. In the first one we would use request.args.get (‘<argument name>’) where request is the instance of the class request imported from Flask.

Is flask a good framework for a simple REST API?

We are building a simple RESTful API, thus Flask is perfect for our needs. Per its official definition, Flask is a lightweight WSGI web application framework. It is designed to make getting started quick and easy. Flask offers suggestions, but doesn’t enforce any dependencies or a project layout.

How to create a resource that supports GET request in flask?

I want to create a resource that supports GET request in following way: 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') Thanks!


2 Answers

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

like image 169
Toby Weed Avatar answered Oct 23 '22 09:10

Toby Weed


The values you are passing are not in the query string. You are using route parts as parameters so the route does not get matched unless you supply something of the form /events/some_name/2019-86-98/2100-9885-24515. The attempts you made are catched by flask as non matching routes and you get 404 not found.

To send those parameters as part of the query string would be to use something like /events?name=some_name&start_date=2019-86-98&end_date=2100-9885-24515 and to get the values in your route handlers you should use name = request.args.get('name') and so on. Passing null values then means hitting the same url but without the query parameters.

like image 22
Gastón Avila Avatar answered Oct 23 '22 08:10

Gastón Avila