flask-restplus
flask-restplus copied to clipboard
flask request RequestParser bundle error=True is not working as expected
from flask import Flask
from flask_restplus import Api, Resource, reqparse
app = Flask(name)
api = Api(app)
parser = reqparse.RequestParser(bundle_errors=False)
parser.add_argument('username', type=list, required=True, help="Missing Username", location="json")
parser.add_argument('password', type=list, required=True, help="Missing Password", location="json")
@api.route('/user')
class User(Resource):
def post(self):
args = parser.parse_args()
return {"ID":"1", "Username": args['username'], "Password": args['password']}, 201
if name == 'main':
app.run(host="0.0.0.0", debug=True)
1- When bundle_errors=False and I send a request with missing parameters
curl -X POST
http://localhost:5051/user
-H 'content-type: application/json'
-d '{}'
I get the following response
{
"errors": {
"username": "Missing Username"
},
"message": "Input payload validation failed"
}
Which is fine except that is showed only one missing field.
2- When I used bundle_errors=True (as mentioned in the documentation), I got the following result
{
"Username": "Missing required parameter in the JSON body",
"Password": "Missing required parameter in the JSON body",
"ID": "1"
}
Which means that RequestParser didn't throw any error and returned this string "Missing required parameter in the JSON body" as the actual input
Am I doing something wrong?
@noirbizarre
Could you format your code and JSON output using triple-backtick code blocks? That'd make it a lot easier to read :)
@TotempaaltJ can you please check now. I have reformatted my code
Doesn't look like you're doing anything wrong, seems to be a bug :(
that's what I want show. is this bug solved or not? @TotempaaltJ
Not that I know of. Have you updated to the latest release (0.11.0)?
bump. any update here?
Try adding the parser part inside "try and catch"
from flask import Flask
from flask_restful import Resource, Api, reqparse
parser = reqparse.RequestParser()
class SampleResource(Resource):
def get(self):
try:
parser.add_argument('user_email', type=str, required=True)
parser.add_argument('user_pass', type=str, required=True)
args = parser.parse_args()
return {
'Email' : str(args['user_email']),
'Password' : str(args['user_pass']),
}, 200
except Exception as e:
return {
'message' : str(e)
}, 400
api.add_resource(SampleResource, '/sample')
if __name__ == '__main__':
app.run(debug = True)
This worked for me