# Response
すべてのハンドラ はレスポンス・オブジェクトを返し、ミドルウェアは、オプションで応答オブジェクトを返すことができます。
# Methods
レスポンス・オブジェクトを生成する最も簡単な方法は、9つの便利なメソッドのいずれかを使用することです。
Default Content-Type: text/plain; charset=utf-8
Description: Returns plain text
from sanic.response import text
@app.route("/")
async def handler(request):
return text("Hi 😎")
Default Content-Type: text/html; charset=utf-8
Description: Returns an HTML document
from sanic.response import html
@app.route("/")
async def handler(request):
return html('<!DOCTYPE html><html lang="en"><meta charset="UTF-8"><div>Hi 😎</div>')
Default Content-Type: application/json
Description: Returns a JSON document
from sanic.response import json
@app.route("/")
async def handler(request):
return json({"foo": "bar"})
By default, Sanic ships with ujson
(opens new window) as its JSON encoder of choice. It is super simple to change this if you want.
from orjson import dumps
json({"foo": "bar"}, dumps=dumps)
If ujson
is not installed, it will fall back to the standard library json
module.
You may additionally declare which implementation to use globally across your application at initialization:
from orjson import dumps
app = Sanic(..., dumps=dumps)
Default Content-Type: N/A
Description: Returns a file
from sanic.response import file
@app.route("/")
async def handler(request):
return await file("/path/to/whatever.png")
Sanic will examine the file, and try and guess its mime type and use an appropriate value for the content type. You could be explicit, if you would like:
file("/path/to/whatever.png", mime_type="image/png")
You can also choose to override the file name:
file("/path/to/whatever.png", filename="super-awesome-incredible.png")
Default Content-Type: text/plain; charset=utf-8
Description: Streams data to a client
from sanic.response import stream
@app.route("/")
async def handler(request):
return stream(streaming_fn)
async def streaming_fn(response):
await response.write('foo')
await response.write('bar')
By default, Sanic will stream back to the client using chunked encoding if the client supports it. You can disable this:
stream(streaming_fn, chunked=False)
Default Content-Type: N/A
Description: Streams a file to a client, useful when streaming large files, like a video
from sanic.response import file_stream
@app.route("/")
async def handler(request):
return await file_stream("/path/to/whatever.mp4")
Like the file()
method, file_stream()
will attempt to determine the mime type of the file.
Default Content-Type: application/octet-stream
Description: Send raw bytes without encoding the body
from sanic.response import raw
@app.route("/")
async def handler(request):
return raw(b"raw bytes")
Default Content-Type: text/html; charset=utf-8
Description: Send a 302
response to redirect the client to a different path
from sanic.response import redirect
@app.route("/")
async def handler(request):
return redirect("/login")
Default Content-Type: N/A
Description: For responding with an empty message as defined by RFC 2616 (opens new window)
from sanic.response import empty
@app.route("/")
async def handler(request):
return empty()
Defaults to a 204
status.
# Default status
レスポンスのデフォルトのHTTPステータス・コードは200
です。変更が必要な場合は、レスポンス方式で行うことができます。
@app.post("/")
async def create_new(request):
new_thing = await do_create(request)
return json({"created": True, "id": new_thing.thing_id}, status=201)