Status code of response is incorrect in after_request hook in a bottle app

家住魔仙堡 提交于 2021-01-28 05:00:23

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!