From 66cb2666413d5ab2639eabfe4693e752c6264516 Mon Sep 17 00:00:00 2001 From: Roman Tezikov Date: Sat, 13 Jun 2020 19:02:45 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=9D=20Add=20docs=20for=20`default=5Fre?= =?UTF-8?q?sponse=5Fclass`=20(#1455)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * :heavy_plus_sign: Add docs to default_response_class * :white_check_mark: create a tip * ✅ fixing the tip * :ambulance: grammar * 📝 Update docs for default response class Co-authored-by: Sebastián Ramírez --- docs/en/docs/advanced/custom-response.md | 15 +++++++++++++++ docs_src/custom_response/tutorial010.py | 9 +++++++++ 2 files changed, 24 insertions(+) create mode 100644 docs_src/custom_response/tutorial010.py diff --git a/docs/en/docs/advanced/custom-response.md b/docs/en/docs/advanced/custom-response.md index 545a84436..f753cc300 100644 --- a/docs/en/docs/advanced/custom-response.md +++ b/docs/en/docs/advanced/custom-response.md @@ -203,6 +203,21 @@ File responses will include appropriate `Content-Length`, `Last-Modified` and `E {!../../../docs_src/custom_response/tutorial009.py!} ``` +## Default response class + +When creating a **FastAPI** class instance or an `APIRouter` you can specify which response class to use by default. + +The parameter that defines this is `default_response_class`. + +In the example below, **FastAPI** will use `ORJSONResponse` by default, in all *path operations*, instead of `JSONResponse`. + +```Python hl_lines="2 4" +{!../../../docs_src/custom_response/tutorial010.py!} +``` + +!!! tip + You can still override `response_class` in *path operations* as before. + ## Additional documentation You can also declare the media type and many other details in OpenAPI using `responses`: [Additional Responses in OpenAPI](additional-responses.md){.internal-link target=_blank}. diff --git a/docs_src/custom_response/tutorial010.py b/docs_src/custom_response/tutorial010.py new file mode 100644 index 000000000..57cb06260 --- /dev/null +++ b/docs_src/custom_response/tutorial010.py @@ -0,0 +1,9 @@ +from fastapi import FastAPI +from fastapi.responses import ORJSONResponse + +app = FastAPI(default_response_class=ORJSONResponse) + + +@app.get("/items/") +async def read_items(): + return [{"item_id": "Foo"}]