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

Most upvoted comments

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:

@hook('after_request')
def enable_cors_after_request_hook():
   print("add_cors_headers")
    response.headers['Access-Control-Allow-Origin'] = '*'
    response.headers['Access-Control-Allow-Methods'] = 'GET, POST, PUT, OPTIONS'
    response.headers['Access-Control-Allow-Headers'] = 'Origin, Accept, Content-Type, X-Requested-With, X-CSRF-Token'

@get('/benchmark/<feed>/<strategy>/<name>')
def bench_get(feed, strategy, name):
    return static_file(name + '.json', root="../benchs/{}/{}".format(feed, strategy))

It calls the function but header are not set.