bottle: 'after_request' runs but headers don't get set
import bottle
app = bottle.Bottle()
@app.hook('after_request')
def fn():
print(dict(bottle.response.headers))
bottle.response.headers['abc'] = 'yes'
print(dict(bottle.response.headers))
@app.get('/')
def asdf():
return ''
app.run()
This code runs properly. I’m able to get the headers when I make a request from another program. However if I change the route to
@app.get('/')
def asdf():
bottle.abort(424)
The hook gets run since I can see it in the server process log
Bottle v0.12.13 server starting up (using WSGIRefServer())...
Listening on http://127.0.0.1:8080/
Hit Ctrl-C to quit.
{}
{'abc': 'yes'}
127.0.0.1 - - [15/Jan/2019 21:23:03] "GET / HTTP/1.1" 424 726
but the header is not received in the program which made the call. That program is also quiet simple. curl http://localhost:8080 -D-
I saw #978 and was facing the same problem. Some of the UI frameworks using our APIs break if the error response does not have CORS headers.
Am I doing something wrong?
About this issue
- Original URL
- State: open
- Created 5 years ago
- Comments: 15 (7 by maintainers)
Commits related to this issue
- fix #1125: Make sure after_request can set headers. This is a backport of the 0.13 _handle() logic and only changes undefined/undocumented behavior (for the better). — committed to bottlepy/bottle by defnull 5 years ago
- Added tests for #1125 fix. — committed to bottlepy/bottle by defnull 5 years ago
- Added tests for #1125 fix. — committed to bottlepy/bottle by defnull 5 years ago
- Added tests for #1125 fix. — committed to bottlepy/bottle by defnull 5 years ago
It’s solved in 0.13 but this gets reported quite often for 0.12, so I’ll look into a backport or pull requests fixing this specific issue for 0.12. Let’s keep this issue open for now.
Yes. There is no “dev” branch.
That’s the current master branch.
This issue is still valid in latest stable release which is 0.12.16 at the moment of writting this comment.
Particularly fails when working with static_files:
It calls the function but header are not set.