如何调用在同一Flask应用程序中定义的webservice端点

2024-04-29 07:06:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个Flask应用程序,它使用connexion库创建API

该应用程序将不仅仅是一个web服务,还将是一个前端应用程序,使用自己的API而不是直接调用数据库

如何从Flask应用程序中调用定义为同一应用程序中的web服务的端点

# Create a URL route in our application for "/products"
@app.route('/products')
def products():
    """
    This function just responds to the browser ULR
    localhost:5000/
    :return:        the rendered template 'home.html'
    """
    r = requests.get('api/read/products')
    print(r.text)
    return render_template('products.html', page_title="Regulatory Analytics Home", products="")

这只是产生:

requests.exceptions.MissingSchema: Invalid URL 'api/read/products': No schema supplied. Perhaps you meant http://api/read/products?

将路径更改为:

r = requests.get('/api/read/products')

不起作用。我的products.html文件位于/templates文件夹中,所以我尝试了

r = requests.get('../api/read/products')

这也不起作用,因为我假设我可能需要一个指向products.html服务所在站点根的相对路径