问题
I have the following Bottle application:
from bottle import Bottle, run, request, response, HTTPResponse
APP1 = Bottle()
@APP1.hook('before_request')
def before_request():
print "APP 1 - Before Request {}".format(request.url)
@APP1.hook('after_request')
def after_request():
print "APP 1 - After Request {}".format(request.url)
print "Response status {}".format(response.status_code)
@APP1.route('/hello')
def hello():
return "Hello World!"
@APP1.route('/error')
def error():
raise HTTPResponse(status=400)
if __name__ == "__main__":
run(APP1, host='127.0.0.1', port=8080)
When I hit the following url: http://127.0.0.1:8080/error
I expect a response code of 400 to be printed but instead it prints 200.
APP 1 - Before Request http://localhost:8080/error
APP 1 - After Request http://localhost:8080/error
Response status 200
I know I can use the Plugin concept to implement an around advice but this means we cannot use after_request
event hook to process error status codes. Besides it seems counter intuitive.
Is my setup incorrect? Or is my understanding of after_request
event hook for Bottle incorrect?
Please advice.
来源:https://stackoverflow.com/questions/26849770/status-code-of-response-is-incorrect-in-after-request-hook-in-a-bottle-app