I have code like this:
def delete(self, rid):
parser = reqparse.RequestParser()
parser.add_argument('rating', default=2, type=int, help='blablabla')
args = parser.parse_args()
rating = args['rating']
...
return {'message': message}
This still asks me for the rating
param, and throws 400 Bad Request
if no exist.
Did I miss something?
With resources defined and everything connected with Blueprints, it's time to handle incoming arguments. Flask-RESTful provides a solid tool known as Reqparse for specifying and validating submitted data.
reqparse has been deprecated (https://flask-restful.readthedocs.io/en/latest/reqparse.html):
Flask-RESTful's request parsing interface, reqparse , is modeled after the argparse interface. It's designed to provide simple and uniform access to any variable on the flask.
resource ( Type[Resource] ) – the class name of your resource. urls (str) – one or more url routes to match for the resource, standard flask routing rules apply. Any url variables will be passed to the resource method as args. endpoint (str) – endpoint name (defaults to Resource.
Try required=False
:
parser.add_argument('rating', default=2, required=False, type=int, help='blablabla')
and check for rating
in args
(if 'rating' in args: pass
).
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