2017-04-11 21:34:55 +01:00
|
|
|
"""
|
|
|
|
Modify header or status in response
|
|
|
|
"""
|
|
|
|
|
2021-12-23 22:30:27 +00:00
|
|
|
from sanic import Sanic, response
|
2017-04-11 21:34:55 +01:00
|
|
|
|
2021-12-23 22:30:27 +00:00
|
|
|
app = Sanic("Example")
|
2017-06-01 19:53:05 +01:00
|
|
|
|
2021-12-23 22:30:27 +00:00
|
|
|
|
|
|
|
@app.route("/")
|
2017-04-11 21:34:55 +01:00
|
|
|
def handle_request(request):
|
|
|
|
return response.json(
|
2021-12-23 22:30:27 +00:00
|
|
|
{"message": "Hello world!"},
|
|
|
|
headers={"X-Served-By": "sanic"},
|
|
|
|
status=200,
|
2017-04-11 21:34:55 +01:00
|
|
|
)
|
2017-06-01 19:53:05 +01:00
|
|
|
|
|
|
|
|
2021-12-23 22:30:27 +00:00
|
|
|
@app.route("/unauthorized")
|
2017-04-11 21:34:55 +01:00
|
|
|
def handle_request(request):
|
|
|
|
return response.json(
|
2021-12-23 22:30:27 +00:00
|
|
|
{"message": "You are not authorized"},
|
|
|
|
headers={"X-Served-By": "sanic"},
|
|
|
|
status=404,
|
2017-04-11 21:34:55 +01:00
|
|
|
)
|
|
|
|
|
2021-12-23 22:30:27 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
app.run(host="0.0.0.0", port=8000, debug=True)
|