-1

I am building a user flask application using MySQL and in the user login code i get the error TypeError: string indices must be integers every time.

@app.route('/signin',methods=['GET','POST'])
def signin():
    if request.method == 'POST':
        email = request.method['email']
        password_entered = request.method['password']
        cur = mysql.connection.cursor()
        user = cur.execute("SELECT * FROM users WHERE email = %s",(email))
        if user > 0:
            data = cur.fetchone()
            real_password = data['password']
            name = data['name']
            email = data['email']
            if pbkdf2_sha256.verify(password_entered,real_password):
                session['logged_in'] = True
                session['name'] = name
                session['email'] = email
                flash('Sign In Successful!',category='info')
                return redirect(url_for('index'))
            else:
                flash('Details Incorrect!Please try again.',category='error')
                return render_template('signin.html')
        else:
            flash('User does not exist!Please Register.',category='error')
            return redirect(url_for('signup'))

    return render_template('signin.html')

Python shows that the error is from the line email = request.method['email']. Please assist.

Conrad Max
  • 11
  • 6
  • Try declaring it as an integer earlier in the code. email = int() - if email is your variable – Thomas Mar 18 '20 at 17:07
  • `request.method` is returning a string, and you can't index into a string with another string. For example, `'POST'['EMAIL']` is meaningless – G. Anderson Mar 18 '20 at 17:08
  • Replace `request.method` to `request.values` or `request.json`. It depends on API caller. – Boseong Choi Mar 18 '20 at 17:21

1 Answers1

0

Because request.method is a string according to the documentation and as you use before: if request.method == 'POST':. Therefore, indexing it with "email" is not a sensible request. password_entered = request.method['password'] should create the same error afterwards.

See: https://flask.palletsprojects.com/en/1.1.x/api/#flask.Request.method

You probably want the request.form or values data.

See: https://flask.palletsprojects.com/en/1.1.x/quickstart/#accessing-request-data

Ketzu
  • 660
  • 1
  • 7
  • 12