Skip to content
Advertisement

Validation of WTForms in Flask using Postman

I am trying to Create a user using WTForms. But always I am getting validation error when checked using POSTMAN. I am not sure why this happens

routes.py

users = Blueprint('users', __name__)

@users.route("/register", methods=['GET', 'POST'])
def register():
    if current_user.is_authenticated:
        return jsonify({"user":"already signed in"})
    form = CreateUserForm(request.form)
    if form.validate():
        hashed_password = bcrypt.generate_password_hash(form.password.data).decode('utf-8')
        user = User(username=form.username.data, email=form.email.data, password=hashed_password, 
                        role=form.role.data, activation_code=form.activation_code.form)
        db.session.add(user)
        db.session.commit()
        flash('Your account has been created! You are now able to log in', 'success')
        return jsonify({"message":"user auth success"})
    return jsonify({"message":"validation failed"})

forms.py

class CreateUserForm(FlaskForm):
    username = StringField('username',
                           validators=[DataRequired(), Length(min=2, max=20)])
    email = StringField('email',
                        validators=[DataRequired(), Email()])
    password = PasswordField('password', validators=[DataRequired()])
    confirm_password = PasswordField('confirm_password',
                                     validators=[DataRequired(), EqualTo('password')])
    role = StringField('role',
                           validators=[DataRequired()])
    activation_code = StringField('activation_code',
                           validators=[DataRequired()])
    # submit = SubmitField('Sign Up')

    def validate_username(self, username):
        user = User.query.filter_by(username=username.data).first()
        if user:
            raise ValidationError('That username is taken. Please choose a different one.')

    def validate_email(self, email):
        user = User.query.filter_by(email=email.data).first()
        if user:
            raise ValidationError('That email is taken. Please choose a different one.')

models.py

class User(db.Model, UserMixin):
    __tablename__ = 'users'
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(20))
    password = db.Column(db.String(255))
    email = db.Column(db.String(100), unique = True)
    image_file = db.Column(db.String(20), nullable=False, default='default.jpg')
    role = db.Column(db.String(6), default = 'Member')
    activation_code = db.Column(db.String(255), default = None)
    rememberme = db.Column(db.String(255))
    reset = db.Column(db.String(255), default = None)
    registered = db.Column(db.String(15), default = time_format())
    lastseen = db.Column(db.String(15), default = time_format(), onupdate = time_format())
    tfa_code = db.Column(db.String(255), default = None)
    ip = db.Column(db.String(255), default = None)
    

    def __init__(self, username, password, email, role, activation_code):
        self.username = username
        self.set_password(password)
        self.email = email
        self.role = role
        self.activation_code = activation_code

Postman screen shot

enter image description here

what can be the reason for validation always failing in this case. Thanks in advance

Advertisement

Answer

For this to work from POSTMAN, set the config

WTF_CSRF_ENABLED = False
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement