Skip to content

Instantly share code, notes, and snippets.

@wshayes
Created July 24, 2019 09:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wshayes/da8a11ca3cbc0f91d0591f5ec990a543 to your computer and use it in GitHub Desktop.
Save wshayes/da8a11ca3cbc0f91d0591f5ec990a543 to your computer and use it in GitHub Desktop.
[FastAPI overriding response example] #fastapi
# https://gitter.im/tiangolo/fastapi?at=5d381bbd5ea6e644ec0d620c
import json
import typing
from starlette.responses import Response
class PrettyJSONResponse(Response):
media_type = "application/json"
def render(self, content: typing.Any) -> bytes:
return json.dumps(
content,
ensure_ascii=False,
allow_nan=False,
indent=4,
separators=(", ", ": "),
).encode("utf-8")
# then using it would look like
@app.get("/", response_class=PrettyJSONResponse)
async def get_some_json():
# ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment