-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CORS does not work with OpenAPI #47
Comments
Hey @GasperFunda, could you provide a code example? |
Here for example I could not reach the login endpoint via my React app due to CORS. When I changed back to normal Flask, everything worked fine. |
@GasperFunda I didn't repeat your bug. server: from flask_cors import CORS, cross_origin
from flask_openapi3 import Info, Tag
from flask_openapi3 import OpenAPI
info = Info(title="Users Microservice for Bank system cr23", version="1.0.0")
app = OpenAPI(__name__, info=info)
cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'
login_tag = Tag(name="Login", description="Logs user into the bank system.")
@app.post('/login', tags=[login_tag])
@cross_origin()
def login():
return {"message": "Invalid username"}, 200
if __name__ == "__main__":
app.run("0.0.0.0", debug=True) js in console: var url = "http://localhost:5000/login";
var httpRequest = new XMLHttpRequest();
httpRequest.open('POST', url, true);
httpRequest.setRequestHeader("Content-type", "application/json");
var obj = {
"username": "mkii",
"password": "1234"
};
httpRequest.send(JSON.stringify(obj));
// 响应后的回调函数
httpRequest.onreadystatechange = function () {
if (httpRequest.readyState == 4 && httpRequest.status == 200) {
var json = httpRequest.responseText;
console.log(json);
}
}; server without CORS: from flask_cors import CORS, cross_origin
from flask_openapi3 import Info, Tag
from flask_openapi3 import OpenAPI
info = Info(title="Users Microservice for Bank system cr23", version="1.0.0")
app = OpenAPI(__name__, info=info)
# cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'
login_tag = Tag(name="Login", description="Logs user into the bank system.")
@app.post('/login', tags=[login_tag])
# @cross_origin()
def login():
return {"message": "Invalid username"}, 200
if __name__ == "__main__":
app.run("0.0.0.0", debug=True) |
This issue has been automatically closed because we haven't heard back for more than 365 days, please reopen this issue if necessary. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
With using flask-cors I cannot reach my endpoints via my web app. If I change to normal Flask cors settings work.
Environment:
The text was updated successfully, but these errors were encountered: