自定义响应类 - 文件、HTML、重定向、流等¶
您可以使用几个自定义响应类来创建一个实例,并直接从您的路径操作中返回它们。
在FastAPI 文档中了解有关自定义响应 - HTML、流、文件、其他的更多信息。
您可以直接从fastapi.responses
中导入它们。
from fastapi.responses import (
FileResponse,
HTMLResponse,
JSONResponse,
ORJSONResponse,
PlainTextResponse,
RedirectResponse,
Response,
StreamingResponse,
UJSONResponse,
)
FastAPI 响应¶
有几个自定义 FastAPI 响应类,您可以使用它们来优化 JSON 性能。
fastapi.responses.UJSONResponse ¶
UJSONResponse(
content,
status_code=200,
headers=None,
media_type=None,
background=None,
)
基类: JSONResponse
使用高性能 ujson 库将数据序列化为 JSON 的 JSON 响应。
在FastAPI 文档中了解有关自定义响应 - HTML、流、文件、其他的更多信息。
参数 | 描述 |
---|---|
content
|
类型: |
status_code
|
类型: |
headers
|
类型: |
media_type
|
类型: |
background
|
类型: |
starlette/responses.py
中的源代码
170 171 172 173 174 175 176 177 178 |
|
render ¶
render(content)
参数 | 描述 |
---|---|
content
|
类型: |
源代码位于 fastapi/responses.py
中
31 32 33 |
|
init_headers ¶
init_headers(headers=None)
参数 | 描述 |
---|---|
headers
|
类型: |
starlette/responses.py
中的源代码
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
|
set_cookie ¶
set_cookie(
key,
value="",
max_age=None,
expires=None,
path="/",
domain=None,
secure=False,
httponly=False,
samesite="lax",
)
参数 | 描述 |
---|---|
key
|
类型: |
value
|
类型: |
max_age
|
类型: |
expires
|
类型: |
path
|
类型: |
domain
|
类型: |
secure
|
类型: |
httponly
|
类型: |
samesite
|
类型: |
starlette/responses.py
中的源代码
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
|
delete_cookie ¶
delete_cookie(
key,
path="/",
domain=None,
secure=False,
httponly=False,
samesite="lax",
)
参数 | 描述 |
---|---|
key
|
类型: |
path
|
类型: |
domain
|
类型: |
secure
|
类型: |
httponly
|
类型: |
samesite
|
类型: |
starlette/responses.py
中的源代码
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
|
fastapi.responses.ORJSONResponse ¶
ORJSONResponse(
content,
status_code=200,
headers=None,
media_type=None,
background=None,
)
基类: JSONResponse
使用高性能 orjson 库将数据序列化为 JSON 的 JSON 响应。
在FastAPI 文档中了解有关自定义响应 - HTML、流、文件、其他的更多信息。
参数 | 描述 |
---|---|
content
|
类型: |
status_code
|
类型: |
headers
|
类型: |
media_type
|
类型: |
background
|
类型: |
starlette/responses.py
中的源代码
170 171 172 173 174 175 176 177 178 |
|
render ¶
render(content)
参数 | 描述 |
---|---|
content
|
类型: |
源代码位于 fastapi/responses.py
中
44 45 46 47 48 |
|
init_headers ¶
init_headers(headers=None)
参数 | 描述 |
---|---|
headers
|
类型: |
starlette/responses.py
中的源代码
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
|
set_cookie ¶
set_cookie(
key,
value="",
max_age=None,
expires=None,
path="/",
domain=None,
secure=False,
httponly=False,
samesite="lax",
)
参数 | 描述 |
---|---|
key
|
类型: |
value
|
类型: |
max_age
|
类型: |
expires
|
类型: |
path
|
类型: |
domain
|
类型: |
secure
|
类型: |
httponly
|
类型: |
samesite
|
类型: |
starlette/responses.py
中的源代码
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
|
delete_cookie ¶
delete_cookie(
key,
path="/",
domain=None,
secure=False,
httponly=False,
samesite="lax",
)
参数 | 描述 |
---|---|
key
|
类型: |
path
|
类型: |
domain
|
类型: |
secure
|
类型: |
httponly
|
类型: |
samesite
|
类型: |
starlette/responses.py
中的源代码
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
|
Starlette 响应¶
fastapi.responses.FileResponse ¶
FileResponse(
path,
status_code=200,
headers=None,
media_type=None,
background=None,
filename=None,
stat_result=None,
method=None,
content_disposition_type="attachment",
)
基类: Response
参数 | 描述 |
---|---|
path
|
类型: |
status_code
|
类型: |
headers
|
类型: |
media_type
|
类型: |
background
|
类型: |
filename
|
类型: |
stat_result
|
类型: |
method
|
类型: |
content_disposition_type
|
类型: |
starlette/responses.py
中的源代码
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
|
render ¶
render(content)
参数 | 描述 |
---|---|
content
|
类型: |
starlette/responses.py
中的源代码
44 45 46 47 48 49 |
|
init_headers ¶
init_headers(headers=None)
参数 | 描述 |
---|---|
headers
|
类型: |
starlette/responses.py
中的源代码
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
|
set_cookie ¶
set_cookie(
key,
value="",
max_age=None,
expires=None,
path="/",
domain=None,
secure=False,
httponly=False,
samesite="lax",
)
参数 | 描述 |
---|---|
key
|
类型: |
value
|
类型: |
max_age
|
类型: |
expires
|
类型: |
path
|
类型: |
domain
|
类型: |
secure
|
类型: |
httponly
|
类型: |
samesite
|
类型: |
starlette/responses.py
中的源代码
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
|
delete_cookie ¶
delete_cookie(
key,
path="/",
domain=None,
secure=False,
httponly=False,
samesite="lax",
)
参数 | 描述 |
---|---|
key
|
类型: |
path
|
类型: |
domain
|
类型: |
secure
|
类型: |
httponly
|
类型: |
samesite
|
类型: |
starlette/responses.py
中的源代码
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
|
fastapi.responses.HTMLResponse ¶
HTMLResponse(
content=None,
status_code=200,
headers=None,
media_type=None,
background=None,
)
基类: Response
参数 | 描述 |
---|---|
content
|
类型: |
status_code
|
类型: |
headers
|
类型: |
media_type
|
类型: |
background
|
类型: |
starlette/responses.py
中的源代码
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
|
render ¶
render(content)
参数 | 描述 |
---|---|
content
|
类型: |
starlette/responses.py
中的源代码
44 45 46 47 48 49 |
|
init_headers ¶
init_headers(headers=None)
参数 | 描述 |
---|---|
headers
|
类型: |
starlette/responses.py
中的源代码
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
|
set_cookie ¶
set_cookie(
key,
value="",
max_age=None,
expires=None,
path="/",
domain=None,
secure=False,
httponly=False,
samesite="lax",
)
参数 | 描述 |
---|---|
key
|
类型: |
value
|
类型: |
max_age
|
类型: |
expires
|
类型: |
path
|
类型: |
domain
|
类型: |
secure
|
类型: |
httponly
|
类型: |
samesite
|
类型: |
starlette/responses.py
中的源代码
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
|
delete_cookie ¶
delete_cookie(
key,
path="/",
domain=None,
secure=False,
httponly=False,
samesite="lax",
)
参数 | 描述 |
---|---|
key
|
类型: |
path
|
类型: |
domain
|
类型: |
secure
|
类型: |
httponly
|
类型: |
samesite
|
类型: |
starlette/responses.py
中的源代码
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
|
fastapi.responses.JSONResponse ¶
JSONResponse(
content,
status_code=200,
headers=None,
media_type=None,
background=None,
)
基类: Response
参数 | 描述 |
---|---|
content
|
类型: |
status_code
|
类型: |
headers
|
类型: |
media_type
|
类型: |
background
|
类型: |
starlette/responses.py
中的源代码
170 171 172 173 174 175 176 177 178 |
|
render ¶
render(content)
参数 | 描述 |
---|---|
content
|
类型: |
starlette/responses.py
中的源代码
180 181 182 183 184 185 186 187 |
|
init_headers ¶
init_headers(headers=None)
参数 | 描述 |
---|---|
headers
|
类型: |
starlette/responses.py
中的源代码
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
|
set_cookie ¶
set_cookie(
key,
value="",
max_age=None,
expires=None,
path="/",
domain=None,
secure=False,
httponly=False,
samesite="lax",
)
参数 | 描述 |
---|---|
key
|
类型: |
value
|
类型: |
max_age
|
类型: |
expires
|
类型: |
path
|
类型: |
domain
|
类型: |
secure
|
类型: |
httponly
|
类型: |
samesite
|
类型: |
starlette/responses.py
中的源代码
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
|
delete_cookie ¶
delete_cookie(
key,
path="/",
domain=None,
secure=False,
httponly=False,
samesite="lax",
)
参数 | 描述 |
---|---|
key
|
类型: |
path
|
类型: |
domain
|
类型: |
secure
|
类型: |
httponly
|
类型: |
samesite
|
类型: |
starlette/responses.py
中的源代码
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
|
fastapi.responses.PlainTextResponse ¶
PlainTextResponse(
content=None,
status_code=200,
headers=None,
media_type=None,
background=None,
)
基类: Response
参数 | 描述 |
---|---|
content
|
类型: |
status_code
|
类型: |
headers
|
类型: |
media_type
|
类型: |
background
|
类型: |
starlette/responses.py
中的源代码
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
|
render ¶
render(content)
参数 | 描述 |
---|---|
content
|
类型: |
starlette/responses.py
中的源代码
44 45 46 47 48 49 |
|
init_headers ¶
init_headers(headers=None)
参数 | 描述 |
---|---|
headers
|
类型: |
starlette/responses.py
中的源代码
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
|
set_cookie ¶
set_cookie(
key,
value="",
max_age=None,
expires=None,
path="/",
domain=None,
secure=False,
httponly=False,
samesite="lax",
)
参数 | 描述 |
---|---|
key
|
类型: |
value
|
类型: |
max_age
|
类型: |
expires
|
类型: |
path
|
类型: |
domain
|
类型: |
secure
|
类型: |
httponly
|
类型: |
samesite
|
类型: |
starlette/responses.py
中的源代码
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
|
delete_cookie ¶
delete_cookie(
key,
path="/",
domain=None,
secure=False,
httponly=False,
samesite="lax",
)
参数 | 描述 |
---|---|
key
|
类型: |
path
|
类型: |
domain
|
类型: |
secure
|
类型: |
httponly
|
类型: |
samesite
|
类型: |
starlette/responses.py
中的源代码
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
|
fastapi.responses.RedirectResponse ¶
RedirectResponse(
url, status_code=307, headers=None, background=None
)
基类: Response
参数 | 描述 |
---|---|
url
|
TYPE: |
status_code
|
TYPE: |
headers
|
类型: |
background
|
类型: |
starlette/responses.py
中的源代码
191 192 193 194 195 196 197 198 199 |
|
render ¶
render(content)
参数 | 描述 |
---|---|
content
|
类型: |
starlette/responses.py
中的源代码
44 45 46 47 48 49 |
|
init_headers ¶
init_headers(headers=None)
参数 | 描述 |
---|---|
headers
|
类型: |
starlette/responses.py
中的源代码
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
|
set_cookie ¶
set_cookie(
key,
value="",
max_age=None,
expires=None,
path="/",
domain=None,
secure=False,
httponly=False,
samesite="lax",
)
参数 | 描述 |
---|---|
key
|
类型: |
value
|
类型: |
max_age
|
类型: |
expires
|
类型: |
path
|
类型: |
domain
|
类型: |
secure
|
类型: |
httponly
|
类型: |
samesite
|
类型: |
starlette/responses.py
中的源代码
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
|
delete_cookie ¶
delete_cookie(
key,
path="/",
domain=None,
secure=False,
httponly=False,
samesite="lax",
)
参数 | 描述 |
---|---|
key
|
类型: |
path
|
类型: |
domain
|
类型: |
secure
|
类型: |
httponly
|
类型: |
samesite
|
类型: |
starlette/responses.py
中的源代码
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
|
fastapi.responses.Response ¶
Response(
content=None,
status_code=200,
headers=None,
media_type=None,
background=None,
)
参数 | 描述 |
---|---|
content
|
类型: |
status_code
|
类型: |
headers
|
类型: |
media_type
|
类型: |
background
|
类型: |
starlette/responses.py
中的源代码
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
|
render ¶
render(content)
参数 | 描述 |
---|---|
content
|
类型: |
starlette/responses.py
中的源代码
44 45 46 47 48 49 |
|
init_headers ¶
init_headers(headers=None)
参数 | 描述 |
---|---|
headers
|
类型: |
starlette/responses.py
中的源代码
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
|
set_cookie ¶
set_cookie(
key,
value="",
max_age=None,
expires=None,
path="/",
domain=None,
secure=False,
httponly=False,
samesite="lax",
)
参数 | 描述 |
---|---|
key
|
类型: |
value
|
类型: |
max_age
|
类型: |
expires
|
类型: |
path
|
类型: |
domain
|
类型: |
secure
|
类型: |
httponly
|
类型: |
samesite
|
类型: |
starlette/responses.py
中的源代码
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
|
delete_cookie ¶
delete_cookie(
key,
path="/",
domain=None,
secure=False,
httponly=False,
samesite="lax",
)
参数 | 描述 |
---|---|
key
|
类型: |
path
|
类型: |
domain
|
类型: |
secure
|
类型: |
httponly
|
类型: |
samesite
|
类型: |
starlette/responses.py
中的源代码
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
|
fastapi.responses.StreamingResponse ¶
StreamingResponse(
content,
status_code=200,
headers=None,
media_type=None,
background=None,
)
基类: Response
参数 | 描述 |
---|---|
content
|
TYPE: |
status_code
|
类型: |
headers
|
类型: |
media_type
|
类型: |
background
|
类型: |
starlette/responses.py
中的源代码
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
|
render ¶
render(content)
参数 | 描述 |
---|---|
content
|
类型: |
starlette/responses.py
中的源代码
44 45 46 47 48 49 |
|
init_headers ¶
init_headers(headers=None)
参数 | 描述 |
---|---|
headers
|
类型: |
starlette/responses.py
中的源代码
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
|
set_cookie ¶
set_cookie(
key,
value="",
max_age=None,
expires=None,
path="/",
domain=None,
secure=False,
httponly=False,
samesite="lax",
)
参数 | 描述 |
---|---|
key
|
类型: |
value
|
类型: |
max_age
|
类型: |
expires
|
类型: |
path
|
类型: |
domain
|
类型: |
secure
|
类型: |
httponly
|
类型: |
samesite
|
类型: |
starlette/responses.py
中的源代码
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
|
delete_cookie ¶
delete_cookie(
key,
path="/",
domain=None,
secure=False,
httponly=False,
samesite="lax",
)
参数 | 描述 |
---|---|
key
|
类型: |
path
|
类型: |
domain
|
类型: |
secure
|
类型: |
httponly
|
类型: |
samesite
|
类型: |
starlette/responses.py
中的源代码
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
|