flask-restplus icon indicating copy to clipboard operation
flask-restplus copied to clipboard

flask request RequestParser bundle error=True is not working as expected

Open vimox-shah opened this issue 6 years ago • 8 comments

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?

vimox-shah avatar May 04 '18 05:05 vimox-shah

@noirbizarre

vimox-shah avatar May 04 '18 05:05 vimox-shah

Could you format your code and JSON output using triple-backtick code blocks? That'd make it a lot easier to read :)

martijnarts avatar May 17 '18 18:05 martijnarts

@TotempaaltJ can you please check now. I have reformatted my code

vimox-shah avatar May 18 '18 05:05 vimox-shah

Doesn't look like you're doing anything wrong, seems to be a bug :(

martijnarts avatar May 18 '18 07:05 martijnarts

that's what I want show. is this bug solved or not? @TotempaaltJ

vimox-shah avatar May 18 '18 08:05 vimox-shah

Not that I know of. Have you updated to the latest release (0.11.0)?

martijnarts avatar May 18 '18 08:05 martijnarts

bump. any update here?

fireridlle avatar Jan 14 '21 11:01 fireridlle

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

OmkarArote avatar Oct 12 '21 11:10 OmkarArote