1. 程式人生 > >使用bottle進行web開發(4):HTTPError

使用bottle進行web開發(4):HTTPError

instead bject hat red uil tle ott class not

from bottle import error
@error(404)
def error404(error):
    return Nothing here, sorry

上述代碼,是對404的定義,這裏註意,有一個HTTPError,

HTTPError uses a predefined HTML template to build the body of the response. Instead of using HTTPError you can use response with the appropriate status code and body.

import json
from bottle import run, route, response @route(/text) def get_text(): response.status = 400 return Object already exists with that name @route(/json) def get_json(): response.status = 400 response.content_type = application/json return json.dumps({error: Object already exists with that name
}) # Start bottle server. run(host=0.0.0.0, port=8070, debug=True)

使用bottle進行web開發(4):HTTPError