Created
July 24, 2019 09:07
-
-
Save wshayes/da8a11ca3cbc0f91d0591f5ec990a543 to your computer and use it in GitHub Desktop.
[FastAPI overriding response example] #fastapi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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