我是 Flask 新手,我想从另一个 API(比如 A)多次调用一个 API(比如 B)。为了更清楚地解释,当 A 被调用时我得到一个项目列表。其中每一项都需要完成一项任务 (B)。 B 已经写好了,所以我想,为什么不在 A 中得到的列表中的每个项目上调用 B 呢?就在那时我遇到了redirect
。但在我看到的所有例子中,它都是用在最后的 return
中。语句使其有效地重定向到另一个 URL 1 次。
我想做的是:
# In API A
status = []
for item in items:
status.append(<some_way_to_call_B>(item)) # status will contain the responses of every API call
return Jsonify(status) # front-end shows which were successful and which weren't
可能吗?
最佳答案
您可以使用 python requests
调用端点 B
内的端点 A
例如:
@app.route("/A"):
def somefuncA():
#some stuff to do
return jsonify(result)
@app.route("/B"):
def somefuncB():
#some stuff to do
response = requests.get("<IP>/A")
#use the result as desired
return jsonify(result)
关于python - 从 Flask 中的另一个 API 多次调用一个 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59246613/