This is an simple application that inserts data into a mysql db. The data gets inserted correctly but i am not getting the reponse of my post request but the status is 200.request.form works but i want to send json as payload This is my below route and i believe something is wrong when i am checking if the keys exist in my json payload. i tried using request.json,request.get_json,request.get_json() but i am unable to get rid of this error although the status is 200 but application is not returning any message.
JavaScript
x
48
48
1
@app.route('/pythonlogin/register', methods=['GET', 'POST'])
2
def register():
3
# Output message if something goes wrong..
4
msg = ''
5
# current_time = time.strftime('%Y-%m-%d %H:%M:%S')
6
# Check if "username", "password" and "email" POST requests exist (user submitted form)
7
if request.method == 'POST' and request.get_json().get('name') != None and request.get_json().get('password') != None and request.get_json().get('email') != None and request.get_json().get('size')!= None :
8
data = request.get_json().get
9
# Create variables for easy access
10
name = data('name')
11
password = data('password')
12
email = data('email')
13
size = data('size')
14
# Check if account exists using MySQL
15
cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
16
cursor.execute('SELECT * FROM user WHERE email = %s', (email,))
17
account = cursor.fetchone()
18
print(account)
19
# If account exists show error and validation checks
20
if account:
21
msg = 'Account already exists!'
22
elif not re.match(r'[^@]+@[^@]+.[^@]+', email):
23
msg = 'Invalid email address!'
24
elif not re.match(r'[A-Za-z0-9]+', name):
25
msg = 'Username must contain only characters and numbers!'
26
elif not re.match(r'[0-9]',size):
27
msg = 'Team size accepts only numbers'
28
elif not name or not password or not email or not size:
29
msg = 'Please fill out the form!'
30
else:
31
print('inside else')
32
role = 1
33
# Account doesnt exists and the form data is valid, now insert new account into accounts table
34
cursor.execute('INSERT INTO company VALUES (NULL, %s,NULL, %s, %s)', (name, size,current_time))
35
#cursor.execute('INSERT INTO user VALUES (NULL, %s, %s, NULL,NULL, NULL,NULL, NULL,NULL, NULL)', (email, password,))
36
mysql.connection.commit()
37
cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
38
cursor.execute('SELECT * FROM company WHERE name = %s ' ,(name,))
39
results = cursor.fetchone()
40
comp_id = results['company_id']
41
cursor.execute('INSERT INTO user VALUES (NULL, NULL, %s,%s, %s,NULL, NULL,NULL, %s,%s)', (email, password,role,current_time,comp_id,))
42
mysql.connection.commit()
43
msg = 'You have successfully registered!'
44
else :
45
# Form is empty... (no POST data)
46
msg = 'Please fill out the form!'
47
return {"status":"1","message":msg}
48
this is my below response:
JavaScript
1
2
1
Debugging middleware caught exception in streamed response at a point where response headers were already sent
2
and this
JavaScript
1
2
1
MySQLdb._exceptions.OperationalError: (2006, '').
2
Advertisement
Answer
Eventually i declared this twice
JavaScript
1
2
1
mysql = MySQL(app)
2