Flask Rest API View Returning Invalid Response

Issue

I am trying to test an endpoint in postman for a flask API, and I am having this error below

TypeError: The view function did not return a valid response tuple. The tuple must have the form (body, status, headers), (body, status), or (body, headers).

The function is given below

auth = Blueprint("auth", __name__, url_prefix="/api/v1/auth")

@auth.post('/register')
def register():
    username = request.json['username']
    password = request.json['password']
    email = request.json['email']
    

    if len(password) < 6:

        return jsonify({'error': "Password is too short"}),
        HTTP_400_BAD_REQUEST 

    if len(username) < 3: 
        return jsonify({'error': "User is too short"}),
        HTTP_400_BAD_REQUEST

    if username.isalnum() or " " in username: 
        return jsonify({'error': "User is too short"}),
        HTTP_400_BAD_REQUEST

    if not username.isalnum() or " " in username: 
        return jsonify({'error': "Username should be alphanumeric, also no spaces"}), 
        HTTP_400_BAD_REQUEST 

    if not validators.email(email):
        return jsonify({'error': "Email is not valid"})
        HTTP_400_BAD_REQUEST

    if User.query.filter_by(email=email).first() is not None: 
        return jsonify({'error': "Email is taken"}), HTTP_409_CONFLICT

    if User.query.filter_by(username=username).first() is not None: 
        return jsonify({'error': "username is taken"}), HTTP_409_CONFLICT

        
    pwd_hash=generate_password_hash(password)

    user = User(username=username, password=pwd_hash, email=email)

    db.session.add(user)
    db.session.commit()

    return jsonify({
        'message': "User created", 
        'user': {
            'username': username, 'email': email
        }
    }), HTTP_201_CREATED

The test input on postman to the route http://127.0.0.1:5000/api/v1/auth/test is :

{
    "username": "username",
    "password": "password",
    "email": "[email protected]"
}

Solution

well you forgot to add a , in the below code

    if not validators.email(email):
        return jsonify({'error': "Email is not valid"})
        HTTP_400_BAD_REQUEST

this is causing this Typeerror

just change this to

return jsonify({'error': "Email is not valid"}),
        HTTP_400_BAD_REQUEST

Answered By – sahasrara62

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published