# ๐ ๐จ - ๐ธ, ๐, ๐, ๐
๐ข, **FastAPI** ๐ ๐จ ๐จ โ๏ธ `JSONResponse`.
๐ ๐ช ๐ โซ๏ธ ๐ฌ `Response` ๐ ๐ [๐จ ๐จ ๐](response-directly.md){.internal-link target=_blank}.
โ๏ธ ๐ฅ ๐ ๐จ `Response` ๐, ๐ ๐ ๐ซ ๐ ๐, & ๐งพ ๐ ๐ซ ๐ ๐ (๐ผ, ๐ ๐ฏ "๐ป ๐", ๐บ๐ธ๐ ๐ `Content-Type` ๐ ๐ ๐).
โ๏ธ ๐ ๐ช ๐ฃ `Response` ๐ ๐ ๐ โ๏ธ, *โก ๐ ๏ธ ๐จโ๐จ*.
๐ ๐ ๐ ๐จ โช๏ธโก๏ธ ๐ *โก ๐ ๏ธ ๐ข* ๐ ๐ฎ ๐ ๐ `Response`.
& ๐ฅ ๐ `Response` โ๏ธ ๐ป ๐ป ๐ (`application/json`), ๐ ๐ผ โฎ๏ธ `JSONResponse` & `UJSONResponse`, ๐ฝ ๐ ๐จ ๐ ๐ ๐ (& โฝ) โฎ๏ธ ๐ Pydantic `response_model` ๐ ๐ ๐ฃ *โก ๐ ๏ธ ๐จโ๐จ*.
!!! note
๐ฅ ๐ โ๏ธ ๐จ ๐ โฎ๏ธ ๐
โโ ๐ป ๐, FastAPI ๐ โ ๐ ๐จ โ๏ธ ๐
โโ ๐, โซ๏ธ ๐ ๐ซ ๐ ๐จ ๐ ๐ฎ ๐ ๐ ๐ฉบ.
## โ๏ธ `ORJSONResponse`
๐ผ, ๐ฅ ๐ โ ๐ญ, ๐ ๐ช โ & โ๏ธ `orjson` & โ ๐จ `ORJSONResponse`.
๐ `Response` ๐ (๐ง-๐) ๐ ๐ โ๏ธ & ๐ฃ โซ๏ธ *โก ๐ ๏ธ ๐จโ๐จ*.
โญ ๐จ, ๐จ `Response` ๐ ๐
โฉ ๐ ๐ฌ ๐.
๐ โฉ๏ธ ๐ข, FastAPI ๐ โ ๐ ๐ฌ ๐ & โ ๐ญ โซ๏ธ ๐ป โฎ๏ธ ๐ป, โ๏ธ ๐ [๐ป ๐ ๐ข](../tutorial/encoder.md){.internal-link target=_blank} ๐ฌ ๐ฐ. ๐ โซ๏ธโ โ ๐ ๐จ **โ ๐**, ๐ผ ๐ฝ ๐ท.
โ๏ธ ๐ฅ ๐ ๐ฏ ๐ ๐ ๐ ๐ ๐ฌ **๐ป โฎ๏ธ ๐ป**, ๐ ๐ช ๐ถโโ๏ธ โซ๏ธ ๐ ๐จ ๐ & โ โ ๐ฅ ๐ FastAPI ๐ โ๏ธ ๐ถโโ๏ธ ๐ ๐จ ๐ ๐ `jsonable_encoder` โญ ๐ถโโ๏ธ โซ๏ธ ๐จ ๐.
```Python hl_lines="2 7"
{!../../../docs_src/custom_response/tutorial001b.py!}
```
!!! info
๐ข `response_class` ๐ โ๏ธ ๐ฌ "๐ป ๐" ๐จ.
๐ ๐ผ, ๐บ๐ธ๐ ๐ `Content-Type` ๐ โ `application/json`.
& โซ๏ธ ๐ ๐ โ
๐.
!!! tip
`ORJSONResponse` โณ ๐ด ๐ช FastAPI, ๐ซ ๐.
## ๐ธ ๐จ
๐จ ๐จ โฎ๏ธ ๐ธ ๐ โช๏ธโก๏ธ **FastAPI**, โ๏ธ `HTMLResponse`.
* ๐ `HTMLResponse`.
* ๐ถโโ๏ธ `HTMLResponse` ๐ข `response_class` ๐ *โก ๐ ๏ธ ๐จโ๐จ*.
```Python hl_lines="2 7"
{!../../../docs_src/custom_response/tutorial002.py!}
```
!!! info
๐ข `response_class` ๐ โ๏ธ ๐ฌ "๐ป ๐" ๐จ.
๐ ๐ผ, ๐บ๐ธ๐ ๐ `Content-Type` ๐ โ `text/html`.
& โซ๏ธ ๐ ๐ โ
๐.
### ๐จ `Response`
๐ [๐จ ๐จ ๐](response-directly.md){.internal-link target=_blank}, ๐ ๐ช ๐ ๐จ ๐ ๐ *โก ๐ ๏ธ*, ๐ฌ โซ๏ธ.
๐ ๐ผ โช๏ธโก๏ธ ๐, ๐ฌ `HTMLResponse`, ๐ช ๐ ๐:
```Python hl_lines="2 7 19"
{!../../../docs_src/custom_response/tutorial003.py!}
```
!!! warning
`Response` ๐จ ๐ ๐ *โก ๐ ๏ธ ๐ข* ๐ ๐ซ ๐ ๐ (๐ผ, `Content-Type` ๐ ๐ซ ๐) & ๐ ๐ซ โญ ๐ง ๐ ๐ฉบ.
!!! info
โ๏ธ, โ `Content-Type` ๐, ๐ ๐, โ๏ธ, ๐ ๐ โช๏ธโก๏ธ `Response` ๐ ๐ ๐จ.
### ๐ ๐ & ๐ `Response`
๐ฅ ๐ ๐ ๐ ๐จ โช๏ธโก๏ธ ๐ ๐ข โ๏ธ ๐ ๐ฐ ๐ "๐ป ๐" ๐, ๐ ๐ช โ๏ธ `response_class` ๐ข & ๐จ `Response` ๐.
`response_class` ๐ โคด๏ธ โ๏ธ ๐ด ๐ ๐ *โก ๐ ๏ธ*, โ๏ธ ๐ `Response` ๐ โ๏ธ.
#### ๐จ `HTMLResponse` ๐
๐ผ, โซ๏ธ ๐ช ๐ณ ๐:
```Python hl_lines="7 21 23"
{!../../../docs_src/custom_response/tutorial004.py!}
```
๐ ๐ผ, ๐ข `generate_html_response()` โช ๐ & ๐จ `Response` โฉ๏ธ ๐ฌ ๐ธ `str`.
๐ฌ ๐ ๐ค `generate_html_response()`, ๐ โช ๐ฌ `Response` ๐ ๐ ๐ ๐ข **FastAPI** ๐ญ.
โ๏ธ ๐ ๐ถโโ๏ธ `HTMLResponse` `response_class` ๐โโ๏ธ, **FastAPI** ๐ ๐ญ โ ๐ โซ๏ธ ๐ & ๐ ๐ฉบ ๐ธ โฎ๏ธ `text/html`:
## ๐ช ๐จ
๐ฅ ๐ช ๐จ.
โ๏ธ ๐คฏ ๐ ๐ ๐ช โ๏ธ `Response` ๐จ ๐ณ ๐, โ๏ธ โ ๐ ๐ง-๐.
!!! note "๐ก โน"
๐ ๐ช โ๏ธ `from starlette.responses import HTMLResponse`.
**FastAPI** ๐ ๐ `starlette.responses` `fastapi.responses` ๐ช ๐, ๐ฉโ๐ป. โ๏ธ ๐
๐ช ๐จ ๐ ๐ โช๏ธโก๏ธ ๐.
### `Response`
๐ `Response` ๐, ๐ ๐ ๐จ ๐ โช๏ธโก๏ธ โซ๏ธ.
๐ ๐ช ๐จ โซ๏ธ ๐.
โซ๏ธ ๐ซ ๐ ๐ข:
* `content` - `str` โ๏ธ `bytes`.
* `status_code` - `int` ๐บ๐ธ๐ ๐ ๐.
* `headers` - `dict` ๐ป.
* `media_type` - `str` ๐ค ๐ป ๐. ๐คถ โ. `"text/html"`.
FastAPI (๐ค ๐) ๐ ๐ ๐ ๐-๐ ๐. โซ๏ธ ๐ ๐ ๐-๐ ๐, โ๏ธ ๐ = & ๐ = โ ๐.
```Python hl_lines="1 18"
{!../../../docs_src/response_directly/tutorial002.py!}
```
### `HTMLResponse`
โ โ โ๏ธ ๐ข & ๐จ ๐ธ ๐จ, ๐ โ ๐.
### `PlainTextResponse`
โ โ โ๏ธ ๐ข & ๐จ โ
โ ๐จ.
```Python hl_lines="2 7 9"
{!../../../docs_src/custom_response/tutorial005.py!}
```
### `JSONResponse`
โ ๐ฝ & ๐จ `application/json` ๐ ๐จ.
๐ ๐ข ๐จ โ๏ธ **FastAPI**, ๐ โ ๐.
### `ORJSONResponse`
โฉ ๐ ๐ป ๐จ โ๏ธ `orjson`, ๐ โ ๐.
### `UJSONResponse`
๐ ๐ป ๐จ โ๏ธ `ujson`.
!!! warning
`ujson` ๐ ๐ ๐ ๐ ๐-๐ ๏ธ โ โซ๏ธ ๐ต ๐-๐ผ.
```Python hl_lines="2 7"
{!../../../docs_src/custom_response/tutorial001.py!}
```
!!! tip
โซ๏ธ ๐ช ๐ `ORJSONResponse` ๐ช โฉ ๐.
### `RedirectResponse`
๐จ ๐บ๐ธ๐ โ. โ๏ธ 3๏ธโฃ0๏ธโฃ7๏ธโฃ ๐ ๐ (๐ โ) ๐ข.
๐ ๐ช ๐จ `RedirectResponse` ๐:
```Python hl_lines="2 9"
{!../../../docs_src/custom_response/tutorial006.py!}
```
---
โ๏ธ ๐ ๐ช โ๏ธ โซ๏ธ `response_class` ๐ข:
```Python hl_lines="2 7 9"
{!../../../docs_src/custom_response/tutorial006b.py!}
```
๐ฅ ๐ ๐, โคด๏ธ ๐ ๐ช ๐จ ๐ ๐ โช๏ธโก๏ธ ๐ *โก ๐ ๏ธ* ๐ข.
๐ ๐ผ, `status_code` โ๏ธ ๐ ๐ข 1๏ธโฃ `RedirectResponse`, โ `307`.
---
๐ ๐ช โ๏ธ `status_code` ๐ข ๐ โฎ๏ธ `response_class` ๐ข:
```Python hl_lines="2 7 9"
{!../../../docs_src/custom_response/tutorial006c.py!}
```
### `StreamingResponse`
โ ๐ ๐ โ๏ธ ๐ ๐/๐ป & ๐ ๐จ ๐ช.
```Python hl_lines="2 14"
{!../../../docs_src/custom_response/tutorial007.py!}
```
#### โ๏ธ `StreamingResponse` โฎ๏ธ ๐-๐ ๐
๐ฅ ๐ โ๏ธ ๐-๐ ๐ (โ
๐ ๐จ `open()`), ๐ ๐ช โ ๐ ๐ข ๐ ๐คญ ๐ ๐-๐ ๐.
๐ ๐, ๐ ๐ซ โ๏ธ โ โซ๏ธ ๐ ๐ฅ ๐พ, & ๐ ๐ช ๐ถโโ๏ธ ๐ ๐ ๐ข `StreamingResponse`, & ๐จ โซ๏ธ.
๐ ๐ ๐ ๐ ๐ โฎ๏ธ โ ๐พ, ๐น ๐ญ, & ๐.
```{ .python .annotate hl_lines="2 10-12 14" }
{!../../../docs_src/custom_response/tutorial008.py!}
```
1๏ธโฃ. ๐ ๐ ๐ข. โซ๏ธ "๐ ๐ข" โฉ๏ธ โซ๏ธ ๐ `yield` ๐ ๐.
2๏ธโฃ. โ๏ธ `with` ๐ซ, ๐ฅ โ ๐ญ ๐ ๐-๐ ๐ ๐ช โฎ๏ธ ๐ ๐ข ๐จ. , โฎ๏ธ โซ๏ธ ๐ ๐จ ๐จ.
3๏ธโฃ. ๐ `yield from` ๐ฌ ๐ข ๐ ๐คญ ๐ ๐ ๐ `file_like`. & โคด๏ธ, ๐ ๐ ๐, ๐พ ๐ ๐ ๐ โช๏ธโก๏ธ ๐ ๐ ๐ข.
, โซ๏ธ ๐ ๐ข ๐ ๐จ "๐ญ" ๐ท ๐ณ ๐ ๐.
๐จ โซ๏ธ ๐ ๐, ๐ฅ ๐ช ๐ฎ โซ๏ธ `with` ๐ซ, & ๐ ๐, ๐ ๐ โซ๏ธ ๐ช โฎ๏ธ ๐.
!!! tip
๐ ๐ ๐ฅ ๐ฅ โ๏ธ ๐ฉ `open()` ๐ ๐ซ ๐โ๐ฆบ `async` & `await`, ๐ฅ ๐ฃ โก ๐ ๏ธ โฎ๏ธ ๐ `def`.
### `FileResponse`
๐ ๐ ๐ ๐จ.
โ ๐ โ โ ๐ ๐ ๐ ๐จ ๐:
* `path` - ๐ ๐ ๐.
* `headers` - ๐ ๐ ๐ ๐, ๐.
* `media_type` - ๐ป ๐ค ๐ป ๐. ๐ฅ ๐ข, ๐ โ๏ธ โก ๐ โ๏ธ ๐ ๐ป ๐.
* `filename` - ๐ฅ โ, ๐ ๐ ๐ ๐จ `Content-Disposition`.
๐ ๐จ ๐ ๐ โ `Content-Length`, `Last-Modified` & `ETag` ๐.
```Python hl_lines="2 10"
{!../../../docs_src/custom_response/tutorial009.py!}
```
๐ ๐ช โ๏ธ `response_class` ๐ข:
```Python hl_lines="2 8 10"
{!../../../docs_src/custom_response/tutorial009b.py!}
```
๐ ๐ผ, ๐ ๐ช ๐จ ๐ โก ๐ โช๏ธโก๏ธ ๐ *โก ๐ ๏ธ* ๐ข.
## ๐ ๐จ ๐
๐ ๐ช โ ๐ ๐ ๐ ๐จ ๐, ๐ โช๏ธโก๏ธ `Response` & โ๏ธ โซ๏ธ.
๐ผ, โก๏ธ ๐ฌ ๐ ๐ ๐ โ๏ธ `orjson`, โ๏ธ โฎ๏ธ ๐ โ ๐ซ โ๏ธ ๐ `ORJSONResponse` ๐.
โก๏ธ ๐ฌ ๐ ๐ โซ๏ธ ๐จ ๐ & ๐ ๐ป, ๐ ๐ โ๏ธ Orjson ๐ `orjson.OPT_INDENT_2`.
๐ ๐ช โ `CustomORJSONResponse`. ๐ ๐ ๐ โ๏ธ โ `Response.render(content)` ๐ฉโ๐ฌ ๐ ๐จ ๐ `bytes`:
```Python hl_lines="9-14 17"
{!../../../docs_src/custom_response/tutorial009c.py!}
```
๐ โฉ๏ธ ๐ฌ:
```json
{"message": "Hello World"}
```
...๐ ๐จ ๐ ๐จ:
```json
{
"message": "Hello World"
}
```
โ๏ธ, ๐ ๐ ๐ฒ ๐ ๐
๐ ๐ โ ๐ ๐ ๐ โ ๐ป. ๐ถ
## ๐ข ๐จ ๐
๐โ ๐ **FastAPI** ๐ ๐ โ๏ธ `APIRouter` ๐ ๐ช โ โ ๐จ ๐ โ๏ธ ๐ข.
๐ข ๐ ๐ฌ ๐ `default_response_class`.
๐ผ ๐, **FastAPI** ๐ โ๏ธ `ORJSONResponse` ๐ข, ๐ *โก ๐ ๏ธ*, โฉ๏ธ `JSONResponse`.
```Python hl_lines="2 4"
{!../../../docs_src/custom_response/tutorial010.py!}
```
!!! tip
๐ ๐ช ๐ `response_class` *โก ๐ ๏ธ* โญ.
## ๐ ๐งพ
๐ ๐ช ๐ฃ ๐ป ๐ & ๐ ๐ โน ๐ โ๏ธ `responses`: [๐ ๐จ ๐](additional-responses.md){.internal-link target=_blank}.