I'm starting with Sanic...
Sanic is a Flask-like Python 3.5+ web server that's written to go fast. (...) On top of being Flask-like, Sanic supports async request handlers. This means you can use the new shiny async/await syntax from Python 3.5, making your code non-blocking and speedy.
... and until this point, there are very few examples on how to use him and the docs are not so good.
Following the docs basic example, we have
from sanic import Sanic
from sanic.response import json
app = Sanic()
@app.route("/")
async def test(request):
return json({"test": True})
if __name__ == '__main__':
app.run(host="0.0.0.0", port=8000)
How can I return a custom response with a custom status code, for example?
In Sanic the HTTP responses are instances of HTTPResponse, as you can see in its below code implementation, and the functions
json,textandhtmljust encapsulated the object creation, following the factory patternThe function
json({"test": True})just dumps adictobject as a JSON string using the ultra fast ujson and set thecontent_typeparam.So you can return a custom status code returning
json({"message": "bla"}, status=201)or creating aHTTPResponseas the above code.