Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
163 commits
Select commit Hold shift + click to select a range
2637665
New translations additional-responses.md (Chinese Simplified)
Sefank Jul 26, 2023
fc7c5bd
New translations additional-status-codes.md (Chinese Simplified)
Sefank Jul 26, 2023
98209f5
New translations advanced-dependencies.md (Chinese Simplified)
Sefank Jul 26, 2023
b890d05
New translations async-sql-databases.md (Chinese Simplified)
Sefank Jul 26, 2023
6369a0e
New translations async-tests.md (Chinese Simplified)
Sefank Jul 26, 2023
a745dcb
New translations behind-a-proxy.md (Chinese Simplified)
Sefank Jul 26, 2023
896bb02
New translations conditional-openapi.md (Chinese Simplified)
Sefank Jul 26, 2023
b54983e
New translations custom-request-and-route.md (Chinese Simplified)
Sefank Jul 26, 2023
94f439a
New translations custom-response.md (Chinese Simplified)
Sefank Jul 26, 2023
faf3788
New translations dataclasses.md (Chinese Simplified)
Sefank Jul 26, 2023
6db8453
New translations events.md (Chinese Simplified)
Sefank Jul 26, 2023
d207dce
New translations extending-openapi.md (Chinese Simplified)
Sefank Jul 26, 2023
8625445
New translations generate-clients.md (Chinese Simplified)
Sefank Jul 26, 2023
6ca3045
New translations graphql.md (Chinese Simplified)
Sefank Jul 26, 2023
66fbb1c
New translations index.md (Chinese Simplified)
Sefank Jul 26, 2023
c48d45a
New translations middleware.md (Chinese Simplified)
Sefank Jul 26, 2023
47303c2
New translations nosql-databases.md (Chinese Simplified)
Sefank Jul 26, 2023
52f74c0
New translations openapi-callbacks.md (Chinese Simplified)
Sefank Jul 26, 2023
b450dc8
New translations openapi-webhooks.md (Chinese Simplified)
Sefank Jul 26, 2023
1d3a76e
New translations path-operation-advanced-configuration.md (Chinese Si…
Sefank Jul 26, 2023
439b6fa
New translations response-change-status-code.md (Chinese Simplified)
Sefank Jul 26, 2023
af622eb
New translations response-cookies.md (Chinese Simplified)
Sefank Jul 26, 2023
751f7cf
New translations response-directly.md (Chinese Simplified)
Sefank Jul 26, 2023
1f9f7f9
New translations response-headers.md (Chinese Simplified)
Sefank Jul 26, 2023
659550f
New translations http-basic-auth.md (Chinese Simplified)
Sefank Jul 26, 2023
17b5f9b
New translations index.md (Chinese Simplified)
Sefank Jul 26, 2023
5076bd2
New translations oauth2-scopes.md (Chinese Simplified)
Sefank Jul 26, 2023
6fe3493
New translations settings.md (Chinese Simplified)
Sefank Jul 26, 2023
2db2c6d
New translations sql-databases-peewee.md (Chinese Simplified)
Sefank Jul 26, 2023
72897d3
New translations sub-applications.md (Chinese Simplified)
Sefank Jul 26, 2023
e036859
New translations templates.md (Chinese Simplified)
Sefank Jul 26, 2023
bb3c0d8
New translations testing-database.md (Chinese Simplified)
Sefank Jul 26, 2023
1812895
New translations testing-dependencies.md (Chinese Simplified)
Sefank Jul 26, 2023
3c9811f
New translations testing-events.md (Chinese Simplified)
Sefank Jul 26, 2023
9ea57dd
New translations testing-websockets.md (Chinese Simplified)
Sefank Jul 26, 2023
ff6bf94
New translations using-request-directly.md (Chinese Simplified)
Sefank Jul 26, 2023
608d3b8
New translations websockets.md (Chinese Simplified)
Sefank Jul 26, 2023
03a62c7
New translations wsgi.md (Chinese Simplified)
Sefank Jul 26, 2023
66cb27e
New translations alternatives.md (Chinese Simplified)
Sefank Jul 26, 2023
5137ad6
New translations async.md (Chinese Simplified)
Sefank Jul 26, 2023
ca07105
New translations benchmarks.md (Chinese Simplified)
Sefank Jul 26, 2023
3f44414
New translations contributing.md (Chinese Simplified)
Sefank Jul 26, 2023
9f27d8d
New translations concepts.md (Chinese Simplified)
Sefank Jul 26, 2023
dcb1e51
New translations deta.md (Chinese Simplified)
Sefank Jul 26, 2023
a85f630
New translations docker.md (Chinese Simplified)
Sefank Jul 26, 2023
fce41ec
New translations https.md (Chinese Simplified)
Sefank Jul 26, 2023
c5b3e42
New translations index.md (Chinese Simplified)
Sefank Jul 26, 2023
0b088ba
New translations manually.md (Chinese Simplified)
Sefank Jul 26, 2023
f8e81c8
New translations server-workers.md (Chinese Simplified)
Sefank Jul 26, 2023
875dd91
New translations versions.md (Chinese Simplified)
Sefank Jul 26, 2023
82b459d
New translations external-links.md (Chinese Simplified)
Sefank Jul 26, 2023
8ffb26f
New translations fastapi-people.md (Chinese Simplified)
Sefank Jul 26, 2023
23a7cd6
New translations features.md (Chinese Simplified)
Sefank Jul 26, 2023
b4b8044
New translations help-fastapi.md (Chinese Simplified)
Sefank Jul 26, 2023
b549f8f
New translations history-design-future.md (Chinese Simplified)
Sefank Jul 26, 2023
9797d5b
New translations index.md (Chinese Simplified)
Sefank Jul 26, 2023
91e9ecb
New translations newsletter.md (Chinese Simplified)
Sefank Jul 26, 2023
7e4cf98
New translations project-generation.md (Chinese Simplified)
Sefank Jul 26, 2023
f015c66
New translations python-types.md (Chinese Simplified)
Sefank Jul 26, 2023
254be13
New translations background-tasks.md (Chinese Simplified)
Sefank Jul 26, 2023
958cf2f
New translations bigger-applications.md (Chinese Simplified)
Sefank Jul 26, 2023
833551e
New translations body-fields.md (Chinese Simplified)
Sefank Jul 26, 2023
fd156c8
New translations body-multiple-params.md (Chinese Simplified)
Sefank Jul 26, 2023
04ccb7b
New translations body-nested-models.md (Chinese Simplified)
Sefank Jul 26, 2023
1994198
New translations body-updates.md (Chinese Simplified)
Sefank Jul 26, 2023
fb1fa4a
New translations body.md (Chinese Simplified)
Sefank Jul 26, 2023
a6c192d
New translations cookie-params.md (Chinese Simplified)
Sefank Jul 26, 2023
ff6a7e1
New translations cors.md (Chinese Simplified)
Sefank Jul 26, 2023
fdbb610
New translations debugging.md (Chinese Simplified)
Sefank Jul 26, 2023
8c6d11d
New translations classes-as-dependencies.md (Chinese Simplified)
Sefank Jul 26, 2023
fcc5ba1
New translations dependencies-in-path-operation-decorators.md (Chines…
Sefank Jul 26, 2023
61fb1f6
New translations dependencies-with-yield.md (Chinese Simplified)
Sefank Jul 26, 2023
e4f0e47
New translations global-dependencies.md (Chinese Simplified)
Sefank Jul 26, 2023
52f6536
New translations index.md (Chinese Simplified)
Sefank Jul 26, 2023
be1ff5b
New translations sub-dependencies.md (Chinese Simplified)
Sefank Jul 26, 2023
f5f3098
New translations encoder.md (Chinese Simplified)
Sefank Jul 26, 2023
5e77094
New translations extra-data-types.md (Chinese Simplified)
Sefank Jul 26, 2023
b0a0a0c
New translations extra-models.md (Chinese Simplified)
Sefank Jul 26, 2023
4f73128
New translations first-steps.md (Chinese Simplified)
Sefank Jul 26, 2023
7b4c777
New translations handling-errors.md (Chinese Simplified)
Sefank Jul 26, 2023
d51695f
New translations header-params.md (Chinese Simplified)
Sefank Jul 26, 2023
6908883
New translations index.md (Chinese Simplified)
Sefank Jul 26, 2023
f24c22f
New translations metadata.md (Chinese Simplified)
Sefank Jul 26, 2023
1bf9c82
New translations middleware.md (Chinese Simplified)
Sefank Jul 26, 2023
12374d8
New translations path-operation-configuration.md (Chinese Simplified)
Sefank Jul 26, 2023
feaf33d
New translations path-params-numeric-validations.md (Chinese Simplified)
Sefank Jul 26, 2023
43e6359
New translations path-params.md (Chinese Simplified)
Sefank Jul 26, 2023
af859b4
New translations query-params-str-validations.md (Chinese Simplified)
Sefank Jul 26, 2023
c5a5491
New translations query-params.md (Chinese Simplified)
Sefank Jul 26, 2023
fc9dcfe
New translations request-files.md (Chinese Simplified)
Sefank Jul 26, 2023
9d1b1a3
New translations request-forms-and-files.md (Chinese Simplified)
Sefank Jul 26, 2023
8f7116d
New translations request-forms.md (Chinese Simplified)
Sefank Jul 26, 2023
80dfb45
New translations response-model.md (Chinese Simplified)
Sefank Jul 26, 2023
dade6ef
New translations response-status-code.md (Chinese Simplified)
Sefank Jul 26, 2023
7bcf2c3
New translations schema-extra-example.md (Chinese Simplified)
Sefank Jul 26, 2023
ff82fa2
New translations first-steps.md (Chinese Simplified)
Sefank Jul 26, 2023
713063e
New translations get-current-user.md (Chinese Simplified)
Sefank Jul 26, 2023
b2e7ad0
New translations index.md (Chinese Simplified)
Sefank Jul 26, 2023
4155953
New translations oauth2-jwt.md (Chinese Simplified)
Sefank Jul 26, 2023
e628b0a
New translations simple-oauth2.md (Chinese Simplified)
Sefank Jul 26, 2023
02dd4b9
New translations sql-databases.md (Chinese Simplified)
Sefank Jul 26, 2023
2f64a01
New translations static-files.md (Chinese Simplified)
Sefank Jul 26, 2023
6ead130
New translations testing.md (Chinese Simplified)
Sefank Jul 26, 2023
132134c
New translations additional-status-codes.md (Chinese Simplified)
Sefank Jul 26, 2023
f70e7ce
New translations custom-response.md (Chinese Simplified)
Sefank Jul 26, 2023
2c1aaf3
New translations index.md (Chinese Simplified)
Sefank Jul 26, 2023
c49afba
New translations path-operation-advanced-configuration.md (Chinese Si…
Sefank Jul 26, 2023
c4b9a67
New translations response-change-status-code.md (Chinese Simplified)
Sefank Jul 26, 2023
c2adc5e
New translations response-cookies.md (Chinese Simplified)
Sefank Jul 26, 2023
e1ce77d
New translations response-directly.md (Chinese Simplified)
Sefank Jul 26, 2023
5575fd9
New translations response-headers.md (Chinese Simplified)
Sefank Jul 26, 2023
2aab3b1
New translations index.md (Chinese Simplified)
Sefank Jul 26, 2023
99f54ec
New translations settings.md (Chinese Simplified)
Sefank Jul 26, 2023
bcfcd5a
New translations websockets.md (Chinese Simplified)
Sefank Jul 26, 2023
2ce642e
New translations wsgi.md (Chinese Simplified)
Sefank Jul 26, 2023
1e20d99
New translations benchmarks.md (Chinese Simplified)
Sefank Jul 26, 2023
15cd6db
New translations contributing.md (Chinese Simplified)
Sefank Jul 26, 2023
45635a4
New translations fastapi-people.md (Chinese Simplified)
Sefank Jul 26, 2023
3a92dbb
New translations features.md (Chinese Simplified)
Sefank Jul 26, 2023
b0b958e
New translations help-fastapi.md (Chinese Simplified)
Sefank Jul 26, 2023
166e854
New translations bigger-applications.md (Chinese Simplified)
Sefank Jul 26, 2023
e796961
New translations body-fields.md (Chinese Simplified)
Sefank Jul 26, 2023
d33e24d
New translations body-multiple-params.md (Chinese Simplified)
Sefank Jul 26, 2023
0c1f971
New translations body-nested-models.md (Chinese Simplified)
Sefank Jul 26, 2023
ed63cd7
New translations body-updates.md (Chinese Simplified)
Sefank Jul 26, 2023
859556d
New translations body.md (Chinese Simplified)
Sefank Jul 26, 2023
ecaef02
New translations cookie-params.md (Chinese Simplified)
Sefank Jul 26, 2023
a396bd3
New translations cors.md (Chinese Simplified)
Sefank Jul 26, 2023
6193f9e
New translations debugging.md (Chinese Simplified)
Sefank Jul 26, 2023
06f45ba
New translations classes-as-dependencies.md (Chinese Simplified)
Sefank Jul 26, 2023
f5f32f9
New translations dependencies-in-path-operation-decorators.md (Chines…
Sefank Jul 26, 2023
993ae5b
New translations global-dependencies.md (Chinese Simplified)
Sefank Jul 26, 2023
a080f09
New translations index.md (Chinese Simplified)
Sefank Jul 26, 2023
03fa910
New translations sub-dependencies.md (Chinese Simplified)
Sefank Jul 26, 2023
341db04
New translations encoder.md (Chinese Simplified)
Sefank Jul 26, 2023
a945c01
New translations extra-data-types.md (Chinese Simplified)
Sefank Jul 26, 2023
ec22cf1
New translations extra-models.md (Chinese Simplified)
Sefank Jul 26, 2023
48337df
New translations first-steps.md (Chinese Simplified)
Sefank Jul 26, 2023
1c5356f
New translations handling-errors.md (Chinese Simplified)
Sefank Jul 26, 2023
935cf26
New translations header-params.md (Chinese Simplified)
Sefank Jul 26, 2023
81e05aa
New translations index.md (Chinese Simplified)
Sefank Jul 26, 2023
1949c1b
New translations metadata.md (Chinese Simplified)
Sefank Jul 26, 2023
b4af5cf
New translations middleware.md (Chinese Simplified)
Sefank Jul 26, 2023
84da9df
New translations path-operation-configuration.md (Chinese Simplified)
Sefank Jul 26, 2023
651faea
New translations path-params-numeric-validations.md (Chinese Simplified)
Sefank Jul 26, 2023
9841971
New translations path-params.md (Chinese Simplified)
Sefank Jul 26, 2023
d0ea265
New translations query-params-str-validations.md (Chinese Simplified)
Sefank Jul 26, 2023
2e90363
New translations query-params.md (Chinese Simplified)
Sefank Jul 26, 2023
aa73a0e
New translations request-files.md (Chinese Simplified)
Sefank Jul 26, 2023
923cceb
New translations request-forms-and-files.md (Chinese Simplified)
Sefank Jul 26, 2023
6b3d210
New translations request-forms.md (Chinese Simplified)
Sefank Jul 26, 2023
a319c7b
New translations response-model.md (Chinese Simplified)
Sefank Jul 26, 2023
fde986f
New translations response-status-code.md (Chinese Simplified)
Sefank Jul 26, 2023
78743df
New translations schema-extra-example.md (Chinese Simplified)
Sefank Jul 26, 2023
7d821a7
New translations first-steps.md (Chinese Simplified)
Sefank Jul 26, 2023
aaa854a
New translations get-current-user.md (Chinese Simplified)
Sefank Jul 26, 2023
fbb2a99
New translations index.md (Chinese Simplified)
Sefank Jul 26, 2023
52e84d4
New translations oauth2-jwt.md (Chinese Simplified)
Sefank Jul 26, 2023
3795b98
New translations simple-oauth2.md (Chinese Simplified)
Sefank Jul 26, 2023
0f287a6
New translations sql-databases.md (Chinese Simplified)
Sefank Jul 26, 2023
fc7ccbb
New translations static-files.md (Chinese Simplified)
Sefank Jul 26, 2023
aeed608
New translations testing.md (Chinese Simplified)
Sefank Jul 26, 2023
69732d8
New translations index.md (Chinese Simplified)
Sefank Jul 26, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
240 changes: 240 additions & 0 deletions docs/zh/docs/advanced/additional-responses.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
# Additional Responses in OpenAPI

!!! warning
This is a rather advanced topic.

If you are starting with **FastAPI**, you might not need this.

You can declare additional responses, with additional status codes, media types, descriptions, etc.

Those additional responses will be included in the OpenAPI schema, so they will also appear in the API docs.

But for those additional responses you have to make sure you return a `Response` like `JSONResponse` directly, with your status code and content.

## Additional Response with `model`

You can pass to your *path operation decorators* a parameter `responses`.

It receives a `dict`, the keys are status codes for each response, like `200`, and the values are other `dict`s with the information for each of them.

Each of those response `dict`s can have a key `model`, containing a Pydantic model, just like `response_model`.

**FastAPI** will take that model, generate its JSON Schema and include it in the correct place in OpenAPI.

For example, to declare another response with a status code `404` and a Pydantic model `Message`, you can write:

```Python hl_lines="18 22"
{!../../../docs_src/additional_responses/tutorial001.py!}
```

!!! note
Have in mind that you have to return the `JSONResponse` directly.

!!! info
The `model` key is not part of OpenAPI.

**FastAPI** will take the Pydantic model from there, generate the `JSON Schema`, and put it in the correct place.

The correct place is:

* In the key `content`, that has as value another JSON object (`dict`) that contains:
* A key with the media type, e.g. `application/json`, that contains as value another JSON object, that contains:
* A key `schema`, that has as the value the JSON Schema from the model, here's the correct place.
* **FastAPI** adds a reference here to the global JSON Schemas in another place in your OpenAPI instead of including it directly. This way, other applications and clients can use those JSON Schemas directly, provide better code generation tools, etc.

The generated responses in the OpenAPI for this *path operation* will be:

```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"
}
}
}
}
}
}
```

The schemas are referenced to another place inside the OpenAPI schema:

```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"
}
}
}
}
}
}
}
```

## Additional media types for the main response

You can use this same `responses` parameter to add different media types for the same main response.

For example, you can add an additional media type of `image/png`, declaring that your *path operation* can return a JSON object (with media type `application/json`) or a PNG image:

```Python hl_lines="19-24 28"
{!../../../docs_src/additional_responses/tutorial002.py!}
```

!!! note
Notice that you have to return the image using a `FileResponse` directly.

!!! info
Unless you specify a different media type explicitly in your `responses` parameter, FastAPI will assume the response has the same media type as the main response class (default `application/json`).

But if you have specified a custom response class with `None` as its media type, FastAPI will use `application/json` for any additional response that has an associated model.

## Combining information

You can also combine response information from multiple places, including the `response_model`, `status_code`, and `responses` parameters.

You can declare a `response_model`, using the default status code `200` (or a custom one if you need), and then declare additional information for that same response in `responses`, directly in the OpenAPI schema.

**FastAPI** will keep the additional information from `responses`, and combine it with the JSON Schema from your model.

For example, you can declare a response with a status code `404` that uses a Pydantic model and has a custom `description`.

And a response with a status code `200` that uses your `response_model`, but includes a custom `example`:

```Python hl_lines="20-31"
{!../../../docs_src/additional_responses/tutorial003.py!}
```

It will all be combined and included in your OpenAPI, and shown in the API docs:

<img src="/img/tutorial/additional-responses/image01.png" />

## Combine predefined responses and custom ones

You might want to have some predefined responses that apply to many *path operations*, but you want to combine them with custom responses needed by each *path operation*.

For those cases, you can use the Python technique of "unpacking" a `dict` with `**dict_to_unpack`:

```Python
old_dict = {
"old key": "old value",
"second old key": "second old value",
}
new_dict = {**old_dict, "new key": "new value"}
```

Here, `new_dict` will contain all the key-value pairs from `old_dict` plus the new key-value pair:

```Python
{
"old key": "old value",
"second old key": "second old value",
"new key": "new value",
}
```

You can use that technique to re-use some predefined responses in your *path operations* and combine them with additional custom ones.

For example:

```Python hl_lines="13-17 26"
{!../../../docs_src/additional_responses/tutorial004.py!}
```

## More information about OpenAPI responses

To see what exactly you can include in the responses, you can check these sections in the OpenAPI specification:

* <a href="https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#responsesObject" class="external-link" target="_blank">OpenAPI Responses Object</a>, it includes the `Response Object`.
* <a href="https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#responseObject" class="external-link" target="_blank">OpenAPI Response Object</a>, you can include anything from this directly in each response inside your `responses` parameter. Including `description`, `headers`, `content` (inside of this is that you declare different media types and JSON Schemas), and `links`.
58 changes: 47 additions & 11 deletions docs/zh/docs/advanced/additional-status-codes.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,61 @@

例如,假设你想有一个 *路径操作* 能够更新条目,并且更新成功时返回 200 「成功」 的 HTTP 状态码。

但是你也希望它能够接受新的条目。并且当这些条目不存在时,会自动创建并返回 201 「创建」的 HTTP 状态码。
但是你也希望它能够接受新的条目。 并且当这些条目不存在时,会自动创建并返回 201 「创建」的 HTTP 状态码。

要实现它,导入 `JSONResponse`,然后在其中直接返回你的内容,并将 `status_code` 设置为为你要的值。

```Python hl_lines="4 25"
{!../../../docs_src/additional_status_codes/tutorial001.py!}
```
=== "Python 3.10+"

!!! warning "警告"
当你直接返回一个像上面例子中的 `Response` 对象时,它会直接返回。
```Python hl_lines="4 25"
!!! note "技术细节"
你也可以使用 <code>from starlette.responses import JSONResponse</code>。
```
</code>

FastAPI 不会用模型等对该响应进行序列化。
=== "Python 3.9+"

确保其中有你想要的数据,且返回的值为合法的 JSON(如果你使用 `JSONResponse` 的话)。
```Python hl_lines="4 25"
!!! warning "警告"
当你直接返回一个像上面例子中的 <code>Response</code> 对象时,它会直接返回。
```
对象时,它会直接返回。
</code>

!!! note "技术细节"
你也可以使用 `from starlette.responses import JSONResponse`。 
=== "Python 3.6+"

出于方便,**FastAPI** 为开发者提供同 `starlette.responses` 一样的 `fastapi.responses`。但是大多数可用的响应都是直接来自 Starlette。`status` 也是一样。
```Python hl_lines="4 26"
{!> ../../../docs_src/additional_status_codes/tutorial001_an.py!}
```

=== "Python 3.10+ non-Annotated"

!!! tip
Prefer to use the `Annotated` version if possible.

```Python hl_lines="2 23"
{!> ../../../docs_src/additional_status_codes/tutorial001_py310.py!}
```

=== "Python 3.6+ non-Annotated"

!!! tip
Prefer to use the `Annotated` version if possible.

```Python hl_lines="4 25"
{!../../../docs_src/additional_status_codes/tutorial001.py!}
```

!!! warning
When you return a `Response` directly, like in the example above, it will be returned directly.

FastAPI 不会用模型等对该响应进行序列化。 确保其中有你想要的数据,且返回的值为合法的 JSON(如果你使用 `JSONResponse` 的话)。

!!! note "Technical Details"
You could also use `from starlette.responses import JSONResponse`.

出于方便,**FastAPI** 为开发者提供同 `starlette.responses` 一样的 `fastapi.responses`。 但是大多数可用的响应都是直接来自 Starlette。 `status` 也是一样。

## OpenAPI 和 API 文档

Expand Down
Loading