bottlepy / bottle

bottle.py is a fast and simple micro-framework for python web-applications.
http://bottlepy.org/
MIT License
8.33k stars 1.46k forks source link

@hook 'after_request' is not working for errors #1338

Closed mrdc closed 2 years ago

mrdc commented 3 years ago

Hi,

@hook('after_request') is not working for errors: 404, for example.


@hook('after_request')
def enable_cors():
    response.headers['Access-Control-Allow-Origin'] = '*'

@error(404)
def error404(error):
    return '404'
defnull commented 3 years ago

Fixed in master branch, hard to fix in 0.12 without breaking stuff. The problem is that errors or explicitly returning HTTPResponse will bypass any headers already set in the global response object. This is by design. The after_request hook is called too early in 0.12 (before merging the HTTPResponse back into response).