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

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:

主站蜘蛛池模板: 民权县| 新津县| 武安市| 乌鲁木齐市| 霍州市| 分宜县| 和龙市| 同江市| 清河县| 霍城县| 舞阳县| 宁陕县| 双桥区| 井冈山市| 淮安市| 金阳县| 普洱| 寿光市| 台中县| 咸丰县| 视频| 内黄县| 麦盖提县| 马关县| 苏尼特左旗| 子长县| 梓潼县| 漾濞| 金秀| 海伦市| 诸城市| 汕尾市| 汉源县| 静海县| 普洱| 苏尼特右旗| 霍山县| 孟州市| 潜江市| 天长市| 大悟县|