Python Flask RestPlus API에서 BOOL 타입 패러미터 사용

2022. 4. 21. 22:22서버 프로그래밍

FLASK Restplus로 API를 만들때 BOOL 타입 패러미터를 사용하고자 할때에는 다음과 같이 inputs.boolean을 사용해야 한다.

from flask import Flask
from flask_restplus import Resource, Api
from flask_restplus import inputs

app = Flask(__name__)
api = Api(app)

test_parser = api.parser()
test_parser.add_argument('foo', type=inputs.boolean, default=False)
test_parser.add_argument('bar', type=inputs.boolean, default=True)

@api.route('/hello')
class HelloWorld(Resource):
    @api.expect(test_parser)
    def get(self):
        args = test_parser.parse_args()
        return dict(args)

if __name__ == '__main__':
    app.run(debug=True)

https://github.com/noirbizarre/flask-restplus/issues/199

 

RequestParser doesn't handle booleans submitted as 'false' correctly · Issue #199 · noirbizarre/flask-restplus

When the RequestParser is given an argument of type bool, it parses any value as True and only the absence of the parameter as False. If the argument is default=True, it's impossible to actuall...

github.com