Python, Flask: How to set response header for all responses

后端 未结 3 1761
暗喜
暗喜 2020-12-28 14:16

I want to set all of my http headers responses to something like this:

response.headers[\"X-Frame-Options\"] = \"SAMEORIGIN\"

I checked thi

3条回答
  •  天命终不由人
    2020-12-28 14:35

    Set the header in a @app.after_request() hook, at which point you have a response object to set the header on:

    @app.after_request
    def apply_caching(response):
        response.headers["X-Frame-Options"] = "SAMEORIGIN"
        return response
    

    The flask.request context is still available when this hook runs, so you can still vary the response based on the request at this time.

提交回复
热议问题