- Cloud Native Python
- Manish Sethi
- 131字
- 2021-07-02 19:11:58
GET /api/v1/users
The GET/api/v1/users method shows the list of all users.
Let's create an /api/v1/users route by adding the following code snippet to app.py:
@app.route('/api/v1/users', methods=['GET']) def get_users(): return list_users()
Now that we have added the route, we need to define the list_users() function, which will connect with the database to get you the complete list of users. Add the following code to app.py:
def list_users():
conn = sqlite3.connect('mydb.db')
print ("Opened database successfully");
api_list=[]
cursor = conn.execute("SELECT username, full_name,
email, password, id from users")
for row in cursor:
a_dict = {}
a_dict['username'] = row[0]
a_dict['name'] = row[1]
a_dict['email'] = row[2]
a_dict['password'] = row[3]
a_dict['id'] = row[4]
api_list.append(a_dict)
conn.close()
return jsonify({'user_list': api_list})
Now that we have added the route and the handle for it, let's test check the http://localhost:5000/api/v1/users URL as follows:

推薦閱讀
- Oracle WebLogic Server 12c:First Look
- Mastering Julia
- 編寫高質量代碼:改善C程序代碼的125個建議
- Learn Scala Programming
- 深入淺出RxJS
- 程序是怎樣跑起來的(第3版)
- MATLAB for Machine Learning
- RabbitMQ Essentials
- The Professional ScrumMaster’s Handbook
- Java EE企業級應用開發教程(Spring+Spring MVC+MyBatis)
- 深入淺出Go語言編程
- JavaScript應用開發實踐指南
- C語言程序設計與應用(第2版)
- 數據分析與挖掘算法:Python實戰
- Java并發實現原理:JDK源碼剖析