bottle Python HTTP custom response code minimal example
In bottle you can generate a custom HTTP response code by first importing HTTPResponse:
bottle_custom_response.py
from bottle import HTTPResponseand then, in your @route function, raise a HTTPResponse, for example
example.py
raise HTTPResponse("This is a HTTP 401 test URL", status=401)Full example
bottle_custom_server.py
#!/usr/bin/env python3
from bottle import HTTPResponse, route, run
@route('/test401')
def hello():
raise HTTPResponse("This is a HTTP 401 test URL", status=401)
run(host='localhost', port=8080, debug=True)Run this, script, then open http://localhost/test401 which will result in a HTTP 401 (unauthorized) response code with
output.txt
This is a HTTP 401 test URLas response body.
Check out similar posts by category:
Python
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow