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:
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.
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.
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.
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.
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.
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.
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.
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!
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¶m2=value2
Or to pass null values: http://127.0.0.1:5000/events?param=¶m2=value1
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With