Access the response object in a bottlepy after_request hook
bottle, python
Solution
Is there a way to access the response body before sending the response back?
You could write a simple plugin, which (depending on what you're actually trying to do with the response) might be all you need.
Here's an example from the Bottle plugin docs, which sets a request header. It could just as easily manipulate `body`.
from bottle import response, install
import time
def stopwatch(callback):
def wrapper(*args, **kwargs):
start = time.time()
body = callback(*args, **kwargs)
end = time.time()
response.headers['X-Exec-Time'] = str(end - start)
return body
return wrapper
install(stopwatch)
Hope that works for your purposes.
Problem
I have the following web app: ``` import bottle app = bottle.Bottle() @app.route('/ping') def ping(): print 'pong' return 'pong' @app.hook('after_request') def after(): print 'foo' print bottle.response.body if __name__ == "__main__": app.run(host='0.0.0.0', port='9999', server='cherrypy') ``` Is there a way to access the response body before sending the response back? If I start the app and I query `/ping`, I can see in the console that the `ping()` and the `after()` function run in the right sequence ``` $ python bottle_after_request.py Bottle v0.11.6 server starting up (using CherryPyServer())... Listening on http://0.0.0.0:9999/ Hit Ctrl-C to quit. pong foo ``` but when in `after()` I try to access `response.body`, I don't have anything. In Flask the after_request decorated functions take in input the response object so it's easy to access it. How can I do the same in Bottle? Is there something I'm missing?