# ๐ ๐จ ๐
/// warning
๐ ๐ ๐ง โ.
๐ฅ ๐ โถ๏ธ โฎ๏ธ **FastAPI**, ๐ ๐ช ๐ซ ๐ช ๐.
///
๐ ๐ช ๐ฃ ๐ ๐จ, โฎ๏ธ ๐ ๐ ๐, ๐ ๐, ๐, โ๏ธ.
๐ ๐ ๐จ ๐ ๐ ๐ ๐, ๐ซ ๐ ๐ ๐ ๏ธ ๐ฉบ.
โ๏ธ ๐ ๐ ๐จ ๐ โ๏ธ โ ๐ญ ๐ ๐จ `Response` ๐ `JSONResponse` ๐, โฎ๏ธ ๐ ๐ ๐ & ๐.
## ๐ ๐จ โฎ๏ธ `model`
๐ ๐ช ๐ถโโ๏ธ ๐ *โก ๐ ๏ธ ๐จโ๐จ* ๐ข `responses`.
โซ๏ธ ๐จ `dict`, ๐ ๐ ๐ ๐ ๐จ, ๐ `200`, & ๐ฒ ๐ `dict`โ โฎ๏ธ โน ๐ ๐ซ.
๐ ๐ ๐จ `dict`โ ๐ช โ๏ธ ๐ `model`, โ Pydantic ๐ท, ๐ `response_model`.
**FastAPI** ๐ โ ๐ ๐ท, ๐ ๐ฎ ๐ป ๐ & ๐ โซ๏ธ โ ๐ฅ ๐.
๐ผ, ๐ฃ โ1๏ธโฃ ๐จ โฎ๏ธ ๐ ๐ `404` & Pydantic ๐ท `Message`, ๐ ๐ช โ:
{* ../../docs_src/additional_responses/tutorial001.py hl[18,22] *}
/// note
โ๏ธ ๐คฏ ๐ ๐ โ๏ธ ๐จ `JSONResponse` ๐.
///
/// info
`model` ๐ ๐ซ ๐ ๐.
**FastAPI** ๐ โ Pydantic ๐ท โช๏ธโก๏ธ ๐ค, ๐ `JSON Schema`, & ๐ฎ โซ๏ธ โ ๐ฅ.
โ ๐ฅ:
* ๐ `content`, ๐ โ๏ธ ๐ฒ โ1๏ธโฃ ๐ป ๐ (`dict`) ๐ ๐:
* ๐ โฎ๏ธ ๐ป ๐, โ
`application/json`, ๐ ๐ ๐ฒ โ1๏ธโฃ ๐ป ๐, ๐ ๐:
* ๐ `schema`, ๐ โ๏ธ ๐ฒ ๐ป ๐ โช๏ธโก๏ธ ๐ท, ๐ฅ โ ๐ฅ.
* **FastAPI** ๐ฎ ๐ ๐ฅ ๐ ๐ป ๐ โ1๏ธโฃ ๐ฅ ๐ ๐ โฉ๏ธ โ
โซ๏ธ ๐. ๐ ๐, ๐ ๐ธ & ๐ฉโ๐ป ๐ช โ๏ธ ๐ ๐ป ๐ ๐, ๐ ๐ป ๐ โก ๐งฐ, โ๏ธ.
///
๐ ๐จ ๐ ๐ *โก ๐ ๏ธ* ๐:
```JSON hl_lines="3-12"
{
"responses": {
"404": {
"description": "Additional Response",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Message"
}
}
}
},
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Item"
}
}
}
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
}
}
}
}
```
๐ ๐ โ1๏ธโฃ ๐ฅ ๐ ๐ ๐:
```JSON hl_lines="4-16"
{
"components": {
"schemas": {
"Message": {
"title": "Message",
"required": [
"message"
],
"type": "object",
"properties": {
"message": {
"title": "Message",
"type": "string"
}
}
},
"Item": {
"title": "Item",
"required": [
"id",
"value"
],
"type": "object",
"properties": {
"id": {
"title": "Id",
"type": "string"
},
"value": {
"title": "Value",
"type": "string"
}
}
},
"ValidationError": {
"title": "ValidationError",
"required": [
"loc",
"msg",
"type"
],
"type": "object",
"properties": {
"loc": {
"title": "Location",
"type": "array",
"items": {
"type": "string"
}
},
"msg": {
"title": "Message",
"type": "string"
},
"type": {
"title": "Error Type",
"type": "string"
}
}
},
"HTTPValidationError": {
"title": "HTTPValidationError",
"type": "object",
"properties": {
"detail": {
"title": "Detail",
"type": "array",
"items": {
"$ref": "#/components/schemas/ValidationError"
}
}
}
}
}
}
}
```
## ๐ ๐ ๐ ๐ ๐จ
๐ ๐ช โ๏ธ ๐ ๐ `responses` ๐ข ๐ฎ ๐ ๐ ๐ ๐ ๐ ๐จ.
๐ผ, ๐ ๐ช ๐ฎ ๐ ๐ป ๐ `image/png`, ๐ฃ ๐ ๐ *โก ๐ ๏ธ* ๐ช ๐จ ๐ป ๐ (โฎ๏ธ ๐ป ๐ `application/json`) โ๏ธ ๐ฉ๐ด ๐ผ:
{* ../../docs_src/additional_responses/tutorial002.py hl[19:24,28] *}
/// note
๐ ๐ ๐ โ๏ธ ๐จ ๐ผ โ๏ธ `FileResponse` ๐.
///
/// info
๐ฅ ๐ โ ๐ ๐ป ๐ ๐ฏ ๐ `responses` ๐ข, FastAPI ๐ ๐ค ๐จ โ๏ธ ๐ ๐ป ๐ ๐ ๐จ ๐ (๐ข `application/json`).
โ๏ธ ๐ฅ ๐ โ๏ธ โ ๐ ๐จ ๐ โฎ๏ธ `None` ๐ฎ ๐ป ๐, FastAPI ๐ โ๏ธ `application/json` ๐ ๐ ๐จ ๐ โ๏ธ ๐จโ๐ผ ๐ท.
///
## ๐ โน
๐ ๐ช ๐ ๐จ โน โช๏ธโก๏ธ ๐ ๐ฅ, ๐ `response_model`, `status_code`, & `responses` ๐ข.
๐ ๐ช ๐ฃ `response_model`, โ๏ธ ๐ข ๐ ๐ `200` (โ๏ธ ๐ 1๏ธโฃ ๐ฅ ๐ ๐ช), & โคด๏ธ ๐ฃ ๐ โน ๐ ๐ ๐จ `responses`, ๐ ๐ ๐.
**FastAPI** ๐ ๐ง ๐ โน โช๏ธโก๏ธ `responses`, & ๐ โซ๏ธ โฎ๏ธ ๐ป ๐ โช๏ธโก๏ธ ๐ ๐ท.
๐ผ, ๐ ๐ช ๐ฃ ๐จ โฎ๏ธ ๐ ๐ `404` ๐ โ๏ธ Pydantic ๐ท & โ๏ธ ๐ `description`.
& ๐จ โฎ๏ธ ๐ ๐ `200` ๐ โ๏ธ ๐ `response_model`, โ๏ธ ๐ ๐ `example`:
{* ../../docs_src/additional_responses/tutorial003.py hl[20:31] *}
โซ๏ธ ๐ ๐ ๐ & ๐ ๐ ๐, & ๐ฆ ๐ ๏ธ ๐ฉบ:
## ๐ ๐ข ๐จ & ๐ ๐
๐ ๐ช ๐ โ๏ธ ๐ ๐จ ๐ โ ๐ *โก ๐ ๏ธ*, โ๏ธ ๐ ๐ ๐ ๐ซ โฎ๏ธ ๐ ๐จ ๐ ๐ *โก ๐ ๏ธ*.
๐ ๐ผ, ๐ ๐ช โ๏ธ ๐ โ "๐" `dict` โฎ๏ธ `**dict_to_unpack`:
```Python
old_dict = {
"old key": "old value",
"second old key": "second old value",
}
new_dict = {**old_dict, "new key": "new value"}
```
๐ฅ, `new_dict` ๐ ๐ ๐ ๐-๐ฒ ๐ซ โช๏ธโก๏ธ `old_dict` โ ๐ ๐-๐ฒ ๐ซ:
```Python
{
"old key": "old value",
"second old key": "second old value",
"new key": "new value",
}
```
๐ ๐ช โ๏ธ ๐ โ ๐ค-โ๏ธ ๐ข ๐จ ๐ *โก ๐ ๏ธ* & ๐ ๐ซ โฎ๏ธ ๐ ๐ ๐.
๐ผ:
{* ../../docs_src/additional_responses/tutorial004.py hl[13:17,26] *}
## ๐ โน ๐ ๐ ๐จ
๐ โซ๏ธโ โซ๏ธโ ๐ ๐ช ๐ ๐จ, ๐ ๐ช โ
๐ ๐ ๐ ๐ง:
* ๐ ๐จ ๐, โซ๏ธ ๐ `Response Object`.
* ๐ ๐จ ๐, ๐ ๐ช ๐ ๐ณ โช๏ธโก๏ธ ๐ ๐ ๐ ๐จ ๐ ๐ `responses` ๐ข. โ
`description`, `headers`, `content` (๐ ๐ ๐ ๐ ๐ฃ ๐ ๐ ๐ & ๐ป ๐), & `links`.