📝 Add docs for creating a custom Response class (#5331)

This commit is contained in:
Sebastián Ramírez
2022-09-01 11:32:30 +02:00
committed by GitHub
parent b9d7f86743
commit 356a57daa8
3 changed files with 59 additions and 0 deletions

View File

@@ -0,0 +1,19 @@
from typing import Any
import orjson
from fastapi import FastAPI, Response
app = FastAPI()
class CustomORJSONResponse(Response):
media_type = "application/json"
def render(self, content: Any) -> bytes:
assert orjson is not None, "orjson must be installed"
return orjson.dumps(content, option=orjson.OPT_INDENT_2)
@app.get("/", response_class=CustomORJSONResponse)
async def main():
return {"message": "Hello World"}