Benutzerdefinierte Responseklassen – File, HTML, Redirect, Streaming, usw.¶
Es gibt mehrere benutzerdefinierte Responseklassen, von denen Sie eine Instanz erstellen und diese direkt von Ihren Pfadoperationen zurückgeben können.
Lesen Sie mehr darüber in der FastAPI-Dokumentation zu benutzerdefinierten Responses – HTML, Stream, Datei, andere.
Sie können diese direkt von fastapi.responses
importieren:
from fastapi.responses import (
FileResponse,
HTMLResponse,
JSONResponse,
ORJSONResponse,
PlainTextResponse,
RedirectResponse,
Response,
StreamingResponse,
UJSONResponse,
)
FastAPI-Responses¶
Es gibt einige benutzerdefinierte FastAPI-Responseklassen, welche Sie verwenden können, um die JSON-Performanz zu optimieren.
fastapi.responses.UJSONResponse
¶
UJSONResponse(
content,
status_code=200,
headers=None,
media_type=None,
background=None,
)
Bases: JSONResponse
JSON response using the high-performance ujson library to serialize data to JSON.
Read more about it in the FastAPI docs for Custom Response - HTML, Stream, File, others.
PARAMETER | DESCRIPTION |
---|---|
content |
TYPE:
|
status_code |
TYPE:
|
headers |
TYPE:
|
media_type |
TYPE:
|
background |
TYPE:
|
Source code in starlette/responses.py
176 177 178 179 180 181 182 183 184 |
|
render
¶
render(content)
PARAMETER | DESCRIPTION |
---|---|
content |
TYPE:
|
Source code in fastapi/responses.py
31 32 33 |
|
init_headers
¶
init_headers(headers=None)
PARAMETER | DESCRIPTION |
---|---|
headers |
TYPE:
|
Source code in 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 78 79 80 81 82 83 |
|
set_cookie
¶
set_cookie(
key,
value="",
max_age=None,
expires=None,
path="/",
domain=None,
secure=False,
httponly=False,
samesite="lax",
)
PARAMETER | DESCRIPTION |
---|---|
key |
TYPE:
|
value |
TYPE:
|
max_age |
TYPE:
|
expires |
TYPE:
|
path |
TYPE:
|
domain |
TYPE:
|
secure |
TYPE:
|
httponly |
TYPE:
|
samesite |
TYPE:
|
Source code in starlette/responses.py
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 123 124 125 126 127 128 |
|
delete_cookie
¶
delete_cookie(
key,
path="/",
domain=None,
secure=False,
httponly=False,
samesite="lax",
)
PARAMETER | DESCRIPTION |
---|---|
key |
TYPE:
|
path |
TYPE:
|
domain |
TYPE:
|
secure |
TYPE:
|
httponly |
TYPE:
|
samesite |
TYPE:
|
Source code in starlette/responses.py
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
|
fastapi.responses.ORJSONResponse
¶
ORJSONResponse(
content,
status_code=200,
headers=None,
media_type=None,
background=None,
)
Bases: JSONResponse
JSON response using the high-performance orjson library to serialize data to JSON.
Read more about it in the FastAPI docs for Custom Response - HTML, Stream, File, others.
PARAMETER | DESCRIPTION |
---|---|
content |
TYPE:
|
status_code |
TYPE:
|
headers |
TYPE:
|
media_type |
TYPE:
|
background |
TYPE:
|
Source code in starlette/responses.py
176 177 178 179 180 181 182 183 184 |
|
render
¶
render(content)
PARAMETER | DESCRIPTION |
---|---|
content |
TYPE:
|
Source code in fastapi/responses.py
44 45 46 47 48 |
|
init_headers
¶
init_headers(headers=None)
PARAMETER | DESCRIPTION |
---|---|
headers |
TYPE:
|
Source code in 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 78 79 80 81 82 83 |
|
set_cookie
¶
set_cookie(
key,
value="",
max_age=None,
expires=None,
path="/",
domain=None,
secure=False,
httponly=False,
samesite="lax",
)
PARAMETER | DESCRIPTION |
---|---|
key |
TYPE:
|
value |
TYPE:
|
max_age |
TYPE:
|
expires |
TYPE:
|
path |
TYPE:
|
domain |
TYPE:
|
secure |
TYPE:
|
httponly |
TYPE:
|
samesite |
TYPE:
|
Source code in starlette/responses.py
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 123 124 125 126 127 128 |
|
delete_cookie
¶
delete_cookie(
key,
path="/",
domain=None,
secure=False,
httponly=False,
samesite="lax",
)
PARAMETER | DESCRIPTION |
---|---|
key |
TYPE:
|
path |
TYPE:
|
domain |
TYPE:
|
secure |
TYPE:
|
httponly |
TYPE:
|
samesite |
TYPE:
|
Source code in starlette/responses.py
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
|
Starlette-Responses¶
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",
)
Bases: Response
PARAMETER | DESCRIPTION |
---|---|
path |
TYPE:
|
status_code |
TYPE:
|
headers |
TYPE:
|
media_type |
TYPE:
|
background |
TYPE:
|
filename |
TYPE:
|
stat_result |
TYPE:
|
method |
TYPE:
|
content_disposition_type |
TYPE:
|
Source code in starlette/responses.py
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 301 302 303 304 305 306 307 308 309 310 311 312 |
|
render
¶
render(content)
PARAMETER | DESCRIPTION |
---|---|
content |
TYPE:
|
Source code in starlette/responses.py
44 45 46 47 48 49 |
|
init_headers
¶
init_headers(headers=None)
PARAMETER | DESCRIPTION |
---|---|
headers |
TYPE:
|
Source code in 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 78 79 80 81 82 83 |
|
set_cookie
¶
set_cookie(
key,
value="",
max_age=None,
expires=None,
path="/",
domain=None,
secure=False,
httponly=False,
samesite="lax",
)
PARAMETER | DESCRIPTION |
---|---|
key |
TYPE:
|
value |
TYPE:
|
max_age |
TYPE:
|
expires |
TYPE:
|
path |
TYPE:
|
domain |
TYPE:
|
secure |
TYPE:
|
httponly |
TYPE:
|
samesite |
TYPE:
|
Source code in starlette/responses.py
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 123 124 125 126 127 128 |
|
delete_cookie
¶
delete_cookie(
key,
path="/",
domain=None,
secure=False,
httponly=False,
samesite="lax",
)
PARAMETER | DESCRIPTION |
---|---|
key |
TYPE:
|
path |
TYPE:
|
domain |
TYPE:
|
secure |
TYPE:
|
httponly |
TYPE:
|
samesite |
TYPE:
|
Source code in starlette/responses.py
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
|
fastapi.responses.HTMLResponse
¶
HTMLResponse(
content=None,
status_code=200,
headers=None,
media_type=None,
background=None,
)
Bases: Response
PARAMETER | DESCRIPTION |
---|---|
content |
TYPE:
|
status_code |
TYPE:
|
headers |
TYPE:
|
media_type |
TYPE:
|
background |
TYPE:
|
Source code in starlette/responses.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
|
render
¶
render(content)
PARAMETER | DESCRIPTION |
---|---|
content |
TYPE:
|
Source code in starlette/responses.py
44 45 46 47 48 49 |
|
init_headers
¶
init_headers(headers=None)
PARAMETER | DESCRIPTION |
---|---|
headers |
TYPE:
|
Source code in 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 78 79 80 81 82 83 |
|
set_cookie
¶
set_cookie(
key,
value="",
max_age=None,
expires=None,
path="/",
domain=None,
secure=False,
httponly=False,
samesite="lax",
)
PARAMETER | DESCRIPTION |
---|---|
key |
TYPE:
|
value |
TYPE:
|
max_age |
TYPE:
|
expires |
TYPE:
|
path |
TYPE:
|
domain |
TYPE:
|
secure |
TYPE:
|
httponly |
TYPE:
|
samesite |
TYPE:
|
Source code in starlette/responses.py
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 123 124 125 126 127 128 |
|
delete_cookie
¶
delete_cookie(
key,
path="/",
domain=None,
secure=False,
httponly=False,
samesite="lax",
)
PARAMETER | DESCRIPTION |
---|---|
key |
TYPE:
|
path |
TYPE:
|
domain |
TYPE:
|
secure |
TYPE:
|
httponly |
TYPE:
|
samesite |
TYPE:
|
Source code in starlette/responses.py
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
|
fastapi.responses.JSONResponse
¶
JSONResponse(
content,
status_code=200,
headers=None,
media_type=None,
background=None,
)
Bases: Response
PARAMETER | DESCRIPTION |
---|---|
content |
TYPE:
|
status_code |
TYPE:
|
headers |
TYPE:
|
media_type |
TYPE:
|
background |
TYPE:
|
Source code in starlette/responses.py
176 177 178 179 180 181 182 183 184 |
|
render
¶
render(content)
PARAMETER | DESCRIPTION |
---|---|
content |
TYPE:
|
Source code in starlette/responses.py
186 187 188 189 190 191 192 193 |
|
init_headers
¶
init_headers(headers=None)
PARAMETER | DESCRIPTION |
---|---|
headers |
TYPE:
|
Source code in 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 78 79 80 81 82 83 |
|
set_cookie
¶
set_cookie(
key,
value="",
max_age=None,
expires=None,
path="/",
domain=None,
secure=False,
httponly=False,
samesite="lax",
)
PARAMETER | DESCRIPTION |
---|---|
key |
TYPE:
|
value |
TYPE:
|
max_age |
TYPE:
|
expires |
TYPE:
|
path |
TYPE:
|
domain |
TYPE:
|
secure |
TYPE:
|
httponly |
TYPE:
|
samesite |
TYPE:
|
Source code in starlette/responses.py
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 123 124 125 126 127 128 |
|
delete_cookie
¶
delete_cookie(
key,
path="/",
domain=None,
secure=False,
httponly=False,
samesite="lax",
)
PARAMETER | DESCRIPTION |
---|---|
key |
TYPE:
|
path |
TYPE:
|
domain |
TYPE:
|
secure |
TYPE:
|
httponly |
TYPE:
|
samesite |
TYPE:
|
Source code in starlette/responses.py
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
|
fastapi.responses.PlainTextResponse
¶
PlainTextResponse(
content=None,
status_code=200,
headers=None,
media_type=None,
background=None,
)
Bases: Response
PARAMETER | DESCRIPTION |
---|---|
content |
TYPE:
|
status_code |
TYPE:
|
headers |
TYPE:
|
media_type |
TYPE:
|
background |
TYPE:
|
Source code in starlette/responses.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
|
render
¶
render(content)
PARAMETER | DESCRIPTION |
---|---|
content |
TYPE:
|
Source code in starlette/responses.py
44 45 46 47 48 49 |
|
init_headers
¶
init_headers(headers=None)
PARAMETER | DESCRIPTION |
---|---|
headers |
TYPE:
|
Source code in 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 78 79 80 81 82 83 |
|
set_cookie
¶
set_cookie(
key,
value="",
max_age=None,
expires=None,
path="/",
domain=None,
secure=False,
httponly=False,
samesite="lax",
)
PARAMETER | DESCRIPTION |
---|---|
key |
TYPE:
|
value |
TYPE:
|
max_age |
TYPE:
|
expires |
TYPE:
|
path |
TYPE:
|
domain |
TYPE:
|
secure |
TYPE:
|
httponly |
TYPE:
|
samesite |
TYPE:
|
Source code in starlette/responses.py
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 123 124 125 126 127 128 |
|
delete_cookie
¶
delete_cookie(
key,
path="/",
domain=None,
secure=False,
httponly=False,
samesite="lax",
)
PARAMETER | DESCRIPTION |
---|---|
key |
TYPE:
|
path |
TYPE:
|
domain |
TYPE:
|
secure |
TYPE:
|
httponly |
TYPE:
|
samesite |
TYPE:
|
Source code in starlette/responses.py
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
|
fastapi.responses.RedirectResponse
¶
RedirectResponse(
url, status_code=307, headers=None, background=None
)
Bases: Response
PARAMETER | DESCRIPTION |
---|---|
url |
TYPE:
|
status_code |
TYPE:
|
headers |
TYPE:
|
background |
TYPE:
|
Source code in starlette/responses.py
197 198 199 200 201 202 203 204 205 206 207 |
|
render
¶
render(content)
PARAMETER | DESCRIPTION |
---|---|
content |
TYPE:
|
Source code in starlette/responses.py
44 45 46 47 48 49 |
|
init_headers
¶
init_headers(headers=None)
PARAMETER | DESCRIPTION |
---|---|
headers |
TYPE:
|
Source code in 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 78 79 80 81 82 83 |
|
set_cookie
¶
set_cookie(
key,
value="",
max_age=None,
expires=None,
path="/",
domain=None,
secure=False,
httponly=False,
samesite="lax",
)
PARAMETER | DESCRIPTION |
---|---|
key |
TYPE:
|
value |
TYPE:
|
max_age |
TYPE:
|
expires |
TYPE:
|
path |
TYPE:
|
domain |
TYPE:
|
secure |
TYPE:
|
httponly |
TYPE:
|
samesite |
TYPE:
|
Source code in starlette/responses.py
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 123 124 125 126 127 128 |
|
delete_cookie
¶
delete_cookie(
key,
path="/",
domain=None,
secure=False,
httponly=False,
samesite="lax",
)
PARAMETER | DESCRIPTION |
---|---|
key |
TYPE:
|
path |
TYPE:
|
domain |
TYPE:
|
secure |
TYPE:
|
httponly |
TYPE:
|
samesite |
TYPE:
|
Source code in starlette/responses.py
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
|
fastapi.responses.Response
¶
Response(
content=None,
status_code=200,
headers=None,
media_type=None,
background=None,
)
PARAMETER | DESCRIPTION |
---|---|
content |
TYPE:
|
status_code |
TYPE:
|
headers |
TYPE:
|
media_type |
TYPE:
|
background |
TYPE:
|
Source code in starlette/responses.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
|
render
¶
render(content)
PARAMETER | DESCRIPTION |
---|---|
content |
TYPE:
|
Source code in starlette/responses.py
44 45 46 47 48 49 |
|
init_headers
¶
init_headers(headers=None)
PARAMETER | DESCRIPTION |
---|---|
headers |
TYPE:
|
Source code in 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 78 79 80 81 82 83 |
|
set_cookie
¶
set_cookie(
key,
value="",
max_age=None,
expires=None,
path="/",
domain=None,
secure=False,
httponly=False,
samesite="lax",
)
PARAMETER | DESCRIPTION |
---|---|
key |
TYPE:
|
value |
TYPE:
|
max_age |
TYPE:
|
expires |
TYPE:
|
path |
TYPE:
|
domain |
TYPE:
|
secure |
TYPE:
|
httponly |
TYPE:
|
samesite |
TYPE:
|
Source code in starlette/responses.py
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 123 124 125 126 127 128 |
|
delete_cookie
¶
delete_cookie(
key,
path="/",
domain=None,
secure=False,
httponly=False,
samesite="lax",
)
PARAMETER | DESCRIPTION |
---|---|
key |
TYPE:
|
path |
TYPE:
|
domain |
TYPE:
|
secure |
TYPE:
|
httponly |
TYPE:
|
samesite |
TYPE:
|
Source code in starlette/responses.py
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
|
fastapi.responses.StreamingResponse
¶
StreamingResponse(
content,
status_code=200,
headers=None,
media_type=None,
background=None,
)
Bases: Response
PARAMETER | DESCRIPTION |
---|---|
content |
TYPE:
|
status_code |
TYPE:
|
headers |
TYPE:
|
media_type |
TYPE:
|
background |
TYPE:
|
Source code in starlette/responses.py
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
|
render
¶
render(content)
PARAMETER | DESCRIPTION |
---|---|
content |
TYPE:
|
Source code in starlette/responses.py
44 45 46 47 48 49 |
|
init_headers
¶
init_headers(headers=None)
PARAMETER | DESCRIPTION |
---|---|
headers |
TYPE:
|
Source code in 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 78 79 80 81 82 83 |
|
set_cookie
¶
set_cookie(
key,
value="",
max_age=None,
expires=None,
path="/",
domain=None,
secure=False,
httponly=False,
samesite="lax",
)
PARAMETER | DESCRIPTION |
---|---|
key |
TYPE:
|
value |
TYPE:
|
max_age |
TYPE:
|
expires |
TYPE:
|
path |
TYPE:
|
domain |
TYPE:
|
secure |
TYPE:
|
httponly |
TYPE:
|
samesite |
TYPE:
|
Source code in starlette/responses.py
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 123 124 125 126 127 128 |
|
delete_cookie
¶
delete_cookie(
key,
path="/",
domain=None,
secure=False,
httponly=False,
samesite="lax",
)
PARAMETER | DESCRIPTION |
---|---|
key |
TYPE:
|
path |
TYPE:
|
domain |
TYPE:
|
secure |
TYPE:
|
httponly |
TYPE:
|
samesite |
TYPE:
|
Source code in starlette/responses.py
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
|