Replies: 1 comment
|
The A So for application-wide CORS handling, the shape can be simpler: @app.before_request
def cors_preflight():
if request.method == "OPTIONS":
return app.make_default_options_response()
@app.after_request
def cors_headers(response):
response.headers["Access-Control-Allow-Origin"] = "*"
response.headers["Access-Control-Allow-Headers"] = "Content-Type, Authorization"
response.headers["Access-Control-Allow-Methods"] = "GET, POST, OPTIONS, DELETE, PUT"
return responseand the normal view only needs to declare its actual application method: @user_blueprint.route("/signup", methods=["POST"])
def create_user_handler():
return {"message": "registered"}Flask adds automatic OPTIONS support to routes by default. Current routing code records Also note that your If you want full WSGI-level CORS behavior before Flask routing, the outer WSGI middleware is valid too. But for a Flask-level implementation, the hook mechanism itself is not broken; avoid mixing explicit route-level OPTIONS handling with Flask's automatic OPTIONS behavior unless you intentionally want the view to own preflight. |
Uh oh!
There was an error while loading. Please reload this page.
I want to implement CORS processing without any third party libraries (like flask-cors). My desire is simple: assign multiple headers to any response, access and process the pre-flight OPTIONS request.
It can be tested like this
With the middleware, it handles a preflight OPTIONS request as an original one:

But the desired behaviour should be like that (you can comment middleware registration and uncomment logic in the view-handler):

Also, I was able to implement desired functionality with the middleware, but with the Werkzeug one:
So, my question is: what could be wrong with the Flask middleware?
All reactions