简体   繁体   English

API适用于Postman,但不适用于浏览器

[英]API works in Postman, but not on a browser

I created a flask api connecting to my mongodb database. 我创建了一个连接到我的mongodb数据库的烧瓶api。

My initial part of the code looks like: 我的代码的初始部分看起来像:

app = Flask(__name__)
cors = CORS(app, resources={
  r"/api/v1/*": {"origin": "*"},
})
client = MongoClient(connection_str)
db = client.get_database(db_name)

@app.route("/api/v1/players", methods = ['GET'])
def get_all_players():
    ....

This works as I intended when I use Postman, but when I input directly into the browser ( localhost:5000/api/v1/players ), it shows me an error as follows: 当我使用Postman时,这可以正常工作,但是当我直接输入浏览器( localhost:5000/api/v1/players )时,它会显示如下错误:

在此输入图像描述

I think this is the reason why my fetch doesn't work. 我认为这就是为什么我的fetch不起作用的原因。

Any thoughts? 有什么想法吗?

It's the problem with SSL certificate. 这是SSL证书的问题。 All you need to do, is add ssl_context='adhoc' to your app.run() call. 您需要做的就是将ssl_context='adhoc'添加到您的app.run()调用中。

An example : 一个例子 :

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello"

if __name__ == "__main__":
    app.run(ssl_context='adhoc')

also you need to install pyopenssl in your virtual environment 您还需要在虚拟环境中安装pyopenssl

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM