July 2017
Intermediate to advanced
374 pages
8h
English
The PUT API basically helps us update a user's record specified by user_id.
Go ahead and create a route with the PUT method to update the user records defined in the app.py file, as follows:
@app.route('/api/v1/users/<int:user_id>', methods=['PUT'])
def update_user(user_id):
user = {}
if not request.json:
abort(400)
user['id']=user_id
key_list = request.json.keys()
for i in key_list:
user[i] = request.json[i]
print (user)
return jsonify({'status': upd_user(user)}), 200
Let's specify the definition of the upd_user(user) function, which basically updates the information in the database with the check that the user id exists:
def upd_user(user): conn = sqlite3.connect('mydb.db') print ("Opened database successfully"); cursor=conn.cursor() ...