官术网_书友最值得收藏!

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:

主站蜘蛛池模板: 永泰县| 武穴市| 泰和县| 康平县| 商河县| 吉隆县| 勃利县| 三明市| 新昌县| 浠水县| 平原县| 左权县| 甘肃省| 平利县| 丽江市| 淳化县| 思茅市| 竹溪县| 辽源市| 曲水县| 井研县| 阳谷县| 大名县| 北海市| 南城县| 顺义区| 九寨沟县| 筠连县| 武清区| 贵阳市| 怀宁县| 梧州市| 堆龙德庆县| 临夏市| 郓城县| 彰化县| 云霄县| 凉山| 买车| 文成县| 麻栗坡县|