July 2017
Intermediate to advanced
374 pages
8h
English
The GET/api/v1/users/[user_id] method shows the user details defined by user_id.
Let's create the route for preceding a GET request into the app.py file as follows:
@app.route('/api/v1/users/<int:user_id>', methods=['GET'])
def get_user(user_id):
return list_user(user_id)
As you can see in the preceding code, we call the list_user(user_id) route into the list_user(user) function, which is not yet defined in app.py. Let's define it to get the details of the specified user, as follows, in the app.py file:
def list_user(user_id): conn = sqlite3.connect('mydb.db') print ("Opened database successfully"); api_list=[] cursor=conn.cursor() cursor.execute("SELECT * from users where id=?",(user_id,)) data = cursor.fetchall() ...Read now
Unlock full access