我正在使用python Flask框架开发REST API。我没有使用任何外部库来生成我的API端点。我只是注册一个路径 @ app.route并在方法中进行所有数据库调用和jsonification。以下是我的一个api端点的示例:
@app.route('/artists', methods=['GET'])
def getArtists():
con = sqlite3.connect("art.db")
cur = con.cursor()
get_artists_query = '''SELECT artist_id, artist_name, profile_image FROM artwolf LIMIT 20'''
cur.execute(get_artists_query)
artists = []
for row in cur:
artist = {}
artist['artistId'] = row[0]
artist['artistName'] = row[1]
artist['profilePicture'] = row[2]
artists.append(artist)
return jsonify({'artists': artists})
cur.close()
con.close()
如上所示,我已经实现了所有端点。现在,我想向我的API添加过滤参数。例如,我希望有一个用于搜索的API端点,如:www.example.com/search?q=van或者用于过滤的东西。但是我无法为此目的找到任何好的学习资源。我试着注册"?q = van"在@ app.route中,如下所示:
@app.route('/artisto?lim=<int:artist_id>', methods=['GET'])
但它给了我404错误。 所以我的问题是,在Flask中注册查询参数(不是enpoints)的最佳方法是什么。 非常感谢你的帮助。