bottle Python HTTP custom response code minimal example
In bottle you can generate a custom HTTP response code by first importing HTTPResponse
:
example.py
from bottle import HTTPResponse
and 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
example.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
example.txt
This is a HTTP 401 test URL
as 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