Flask-Pydantic 在获取请求时禁用验证
Flask-Pydantic disable validation on get request
我有一个接受 get 和 post 请求的烧瓶视图,我使用 Pydantic 使用 flask-pydantic 进行请求主体验证。它适用于 post 请求,但在 get 请求时 returns 出现 415 错误消息 -
{"detail":"请求中不支持媒体类型''。需要 'application/json'。"}
@bp.route('/login', methods=('GET', 'POST',))
@validate()
def login(body: UserLoginSchema):
if request.method == 'POST':
existing_user = UserListSchema.from_orm(
db_session.query(UserModel.id, UserModel.email, UserModel.first_name, UserModel.last_name,
UserModel.is_admin, UserModel.is_verified, UserModel.password)
.filter_by(email=body.email, is_active=True).first()
)
if existing_user:
if check_password_hash(existing_user.password, body.password):
session.clear()
session['user'] = str(existing_user.json())
return redirect(url_for('index'))
flash('Invalid username or password')
return render_template('auth/login.html')
我试过将函数中的 query 参数设置为空字符串或 None 但没有帮助。
我删除了 flask-pydantic 包并手动初始化了 pydantic 模型,因为来自 flask-pydantic 的验证装饰器需要将 content-type header 设置为 application/json.
@bp.route('/login', methods=('GET', 'POST',))
def login():
if request.method == 'POST':
body = auth_schemas.UserLoginSchema(**request.form)
existing_user = auth_schemas.UserListSchema.from_orm(
db_session.query(UserModel.id, UserModel.email, UserModel.first_name, UserModel.last_name,
UserModel.is_admin, UserModel.is_verified, UserModel.password)
.filter_by(email=body.email, is_active=True).first()
)
if existing_user:
if check_password_hash(existing_user.password, body.password):
session.clear()
session['user'] = str(existing_user.json())
return redirect(url_for('index'))
flash('Invalid username or password')
return render_template('auth/login.html')
然后我创建了一个 ValidationError 处理程序来在初始化 pydantic 模型类时捕获验证错误。
from pydantic import ValidationError
@app.errorhandler(ValidationError)
def handle_pydantic_validation_errors(e):
return jsonify(e.errors())
我有一个接受 get 和 post 请求的烧瓶视图,我使用 Pydantic 使用 flask-pydantic 进行请求主体验证。它适用于 post 请求,但在 get 请求时 returns 出现 415 错误消息 - {"detail":"请求中不支持媒体类型''。需要 'application/json'。"}
@bp.route('/login', methods=('GET', 'POST',))
@validate()
def login(body: UserLoginSchema):
if request.method == 'POST':
existing_user = UserListSchema.from_orm(
db_session.query(UserModel.id, UserModel.email, UserModel.first_name, UserModel.last_name,
UserModel.is_admin, UserModel.is_verified, UserModel.password)
.filter_by(email=body.email, is_active=True).first()
)
if existing_user:
if check_password_hash(existing_user.password, body.password):
session.clear()
session['user'] = str(existing_user.json())
return redirect(url_for('index'))
flash('Invalid username or password')
return render_template('auth/login.html')
我试过将函数中的 query 参数设置为空字符串或 None 但没有帮助。
我删除了 flask-pydantic 包并手动初始化了 pydantic 模型,因为来自 flask-pydantic 的验证装饰器需要将 content-type header 设置为 application/json.
@bp.route('/login', methods=('GET', 'POST',))
def login():
if request.method == 'POST':
body = auth_schemas.UserLoginSchema(**request.form)
existing_user = auth_schemas.UserListSchema.from_orm(
db_session.query(UserModel.id, UserModel.email, UserModel.first_name, UserModel.last_name,
UserModel.is_admin, UserModel.is_verified, UserModel.password)
.filter_by(email=body.email, is_active=True).first()
)
if existing_user:
if check_password_hash(existing_user.password, body.password):
session.clear()
session['user'] = str(existing_user.json())
return redirect(url_for('index'))
flash('Invalid username or password')
return render_template('auth/login.html')
然后我创建了一个 ValidationError 处理程序来在初始化 pydantic 模型类时捕获验证错误。
from pydantic import ValidationError
@app.errorhandler(ValidationError)
def handle_pydantic_validation_errors(e):
return jsonify(e.errors())