July 2017
Intermediate to advanced
374 pages
8h
English
The delete method helps remove a specific record, which is defined by a username. We will pass username as the JSON object that needs to be deleted from the database.
The following code snippet will create a new route in app.py for the DELETE method for users:
@app.route('/api/v1/users', methods=['DELETE'])
def delete_user():
if not request.json or not 'username' in request.json:
abort(400)
user=request.json['username']
return jsonify({'status': del_user(user)}), 200
In the next code snippet, we will call del_user, which deletes the user record specified by username after validating whether it exists or not:
def del_user(del_user): conn = sqlite3.connect('mydb.db') print ("Opened database successfully"); cursor=conn.cursor() ...Read now
Unlock full access