- Cloud Native Python
- Manish Sethi
- 187字
- 2021-07-02 19:11:59
DELETE /api/v1/users
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() cursor.execute("SELECT * from users where username=? ",
(del_user,)) data = cursor.fetchall() print ("Data" ,data) if len(data) == 0: abort(404) else: cursor.execute("delete from users where username==?",
(del_user,)) conn.commit() return "Success"
Great! We have added the route /handler for the DELETE method for the user resource; let's test it using the following test API call:
curl -i -H "Content-Type: application/json" -X delete -d '{
"username":"manish123" }' http://localhost:5000/api/v1/users
Then, hit the user list API (curl http://localhost:5000/api/v1/users) to see if the changes have been made:

Awesome! User deletion is successful.
推薦閱讀
- Facebook Application Development with Graph API Cookbook
- Instant Testing with CasperJS
- Django:Web Development with Python
- Windows Server 2012 Unified Remote Access Planning and Deployment
- MATLAB實(shí)用教程
- 零基礎(chǔ)學(xué)Java程序設(shè)計(jì)
- Hands-On Automation Testing with Java for Beginners
- 深入理解Android:Wi-Fi、NFC和GPS卷
- Unity 2D Game Development Cookbook
- LabVIEW虛擬儀器程序設(shè)計(jì)從入門到精通(第二版)
- 響應(yīng)式Web設(shè)計(jì):HTML5和CSS3實(shí)戰(zhàn)(第2版)
- C++語(yǔ)言程序設(shè)計(jì)
- 玩轉(zhuǎn).NET Micro Framework移植:基于STM32F10x處理器
- 零基礎(chǔ)看圖學(xué)ScratchJr:少兒趣味編程(全彩大字版)
- Processing開發(fā)實(shí)戰(zhàn)