Məzmuna keçin

Custom Response Classes - File, HTML, Redirect, Streaming, etc.

There are several custom response classes you can use to create an instance and return them directly from your path operations.

Read more about it in the FastAPI docs for Custom Response - HTML, Stream, File, others.

You can import them directly from fastapi.responses:

from fastapi.responses import (
    FileResponse,
    HTMLResponse,
    JSONResponse,
    ORJSONResponse,
    PlainTextResponse,
    RedirectResponse,
    Response,
    StreamingResponse,
    UJSONResponse,
)

FastAPI Responses

There are a couple of custom FastAPI response classes, you can use them to optimize JSON performance.

fastapi.responses.UJSONResponse

UJSONResponse(<span data-autorefs-optional="fastapi.responses.UJSONResponse(content)">content</span>, <span data-autorefs-optional="fastapi.responses.UJSONResponse(status_code)">status_code</span>=200, <span data-autorefs-optional="fastapi.responses.UJSONResponse(headers)">headers</span>=None, <span data-autorefs-optional="fastapi.responses.UJSONResponse(media_type)">media_type</span>=None, <span data-autorefs-optional="fastapi.responses.UJSONResponse(background)">background</span>=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: Any

status_code

TYPE: int DEFAULT: 200

headers

TYPE: Mapping[str, str] | None DEFAULT: None

media_type

TYPE: str | None DEFAULT: None

background

TYPE: BackgroundTask | None DEFAULT: None

Source code in starlette/responses.py
175
176
177
178
179
180
181
182
183
def __init__(
    self,
    content: typing.Any,
    status_code: int = 200,
    headers: typing.Mapping[str, str] | None = None,
    media_type: str | None = None,
    background: BackgroundTask | None = None,
) -> None:
    super().__init__(content, status_code, headers, media_type, background)

charset class-attribute instance-attribute

charset = 'utf-8'

status_code instance-attribute

status_code = status_code

media_type class-attribute instance-attribute

media_type = 'application/json'

body instance-attribute

body = render(content)

background instance-attribute

background = background

headers property

headers

render

render(<span data-autorefs-optional="fastapi.responses.UJSONResponse.render(content)">content</span>)
PARAMETER DESCRIPTION
content

TYPE: Any

Source code in fastapi/responses.py
31
32
33
def render(self, content: Any) -> bytes:
    assert ujson is not None, "ujson must be installed to use UJSONResponse"
    return ujson.dumps(content, ensure_ascii=False).encode("utf-8")

init_headers

init_headers(<span data-autorefs-optional="fastapi.responses.UJSONResponse.init_headers(headers)">headers</span>=None)
PARAMETER DESCRIPTION
headers

TYPE: Mapping[str, str] | None DEFAULT: None

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
def init_headers(self, headers: typing.Mapping[str, str] | None = None) -> None:
    if headers is None:
        raw_headers: list[tuple[bytes, bytes]] = []
        populate_content_length = True
        populate_content_type = True
    else:
        raw_headers = [
            (k.lower().encode("latin-1"), v.encode("latin-1"))
            for k, v in headers.items()
        ]
        keys = [h[0] for h in raw_headers]
        populate_content_length = b"content-length" not in keys
        populate_content_type = b"content-type" not in keys

    body = getattr(self, "body", None)
    if (
        body is not None
        and populate_content_length
        and not (self.status_code < 200 or self.status_code in (204, 304))
    ):
        content_length = str(len(body))
        raw_headers.append((b"content-length", content_length.encode("latin-1")))

    content_type = self.media_type
    if content_type is not None and populate_content_type:
        if (
            content_type.startswith("text/")
            and "charset=" not in content_type.lower()
        ):
            content_type += "; charset=" + self.charset
        raw_headers.append((b"content-type", content_type.encode("latin-1")))

    self.raw_headers = raw_headers
set_cookie(<span data-autorefs-optional="fastapi.responses.UJSONResponse.set_cookie(key)">key</span>, <span data-autorefs-optional="fastapi.responses.UJSONResponse.set_cookie(value)">value</span>='', <span data-autorefs-optional="fastapi.responses.UJSONResponse.set_cookie(max_age)">max_age</span>=None, <span data-autorefs-optional="fastapi.responses.UJSONResponse.set_cookie(expires)">expires</span>=None, <span data-autorefs-optional="fastapi.responses.UJSONResponse.set_cookie(path)">path</span>='/', <span data-autorefs-optional="fastapi.responses.UJSONResponse.set_cookie(domain)">domain</span>=None, <span data-autorefs-optional="fastapi.responses.UJSONResponse.set_cookie(secure)">secure</span>=False, <span data-autorefs-optional="fastapi.responses.UJSONResponse.set_cookie(httponly)">httponly</span>=False, <span data-autorefs-optional="fastapi.responses.UJSONResponse.set_cookie(samesite)">samesite</span>='lax')
PARAMETER DESCRIPTION
key

TYPE: str

value

TYPE: str DEFAULT: ''

max_age

TYPE: int | None DEFAULT: None

expires

TYPE: datetime | str | int | None DEFAULT: None

path

TYPE: str DEFAULT: '/'

domain

TYPE: str | None DEFAULT: None

secure

TYPE: bool DEFAULT: False

httponly

TYPE: bool DEFAULT: False

samesite

TYPE: Literal['lax', 'strict', 'none'] | None DEFAULT: 'lax'

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
def set_cookie(
    self,
    key: str,
    value: str = "",
    max_age: int | None = None,
    expires: datetime | str | int | None = None,
    path: str = "/",
    domain: str | None = None,
    secure: bool = False,
    httponly: bool = False,
    samesite: typing.Literal["lax", "strict", "none"] | None = "lax",
) -> None:
    cookie: http.cookies.BaseCookie[str] = http.cookies.SimpleCookie()
    cookie[key] = value
    if max_age is not None:
        cookie[key]["max-age"] = max_age
    if expires is not None:
        if isinstance(expires, datetime):
            cookie[key]["expires"] = format_datetime(expires, usegmt=True)
        else:
            cookie[key]["expires"] = expires
    if path is not None:
        cookie[key]["path"] = path
    if domain is not None:
        cookie[key]["domain"] = domain
    if secure:
        cookie[key]["secure"] = True
    if httponly:
        cookie[key]["httponly"] = True
    if samesite is not None:
        assert samesite.lower() in [
            "strict",
            "lax",
            "none",
        ], "samesite must be either 'strict', 'lax' or 'none'"
        cookie[key]["samesite"] = samesite
    cookie_val = cookie.output(header="").strip()
    self.raw_headers.append((b"set-cookie", cookie_val.encode("latin-1")))
delete_cookie(<span data-autorefs-optional="fastapi.responses.UJSONResponse.delete_cookie(key)">key</span>, <span data-autorefs-optional="fastapi.responses.UJSONResponse.delete_cookie(path)">path</span>='/', <span data-autorefs-optional="fastapi.responses.UJSONResponse.delete_cookie(domain)">domain</span>=None, <span data-autorefs-optional="fastapi.responses.UJSONResponse.delete_cookie(secure)">secure</span>=False, <span data-autorefs-optional="fastapi.responses.UJSONResponse.delete_cookie(httponly)">httponly</span>=False, <span data-autorefs-optional="fastapi.responses.UJSONResponse.delete_cookie(samesite)">samesite</span>='lax')
PARAMETER DESCRIPTION
key

TYPE: str

path

TYPE: str DEFAULT: '/'

domain

TYPE: str | None DEFAULT: None

secure

TYPE: bool DEFAULT: False

httponly

TYPE: bool DEFAULT: False

samesite

TYPE: Literal['lax', 'strict', 'none'] | None DEFAULT: 'lax'

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
def delete_cookie(
    self,
    key: str,
    path: str = "/",
    domain: str | None = None,
    secure: bool = False,
    httponly: bool = False,
    samesite: typing.Literal["lax", "strict", "none"] | None = "lax",
) -> None:
    self.set_cookie(
        key,
        max_age=0,
        expires=0,
        path=path,
        domain=domain,
        secure=secure,
        httponly=httponly,
        samesite=samesite,
    )

fastapi.responses.ORJSONResponse

ORJSONResponse(<span data-autorefs-optional="fastapi.responses.ORJSONResponse(content)">content</span>, <span data-autorefs-optional="fastapi.responses.ORJSONResponse(status_code)">status_code</span>=200, <span data-autorefs-optional="fastapi.responses.ORJSONResponse(headers)">headers</span>=None, <span data-autorefs-optional="fastapi.responses.ORJSONResponse(media_type)">media_type</span>=None, <span data-autorefs-optional="fastapi.responses.ORJSONResponse(background)">background</span>=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: Any

status_code

TYPE: int DEFAULT: 200

headers

TYPE: Mapping[str, str] | None DEFAULT: None

media_type

TYPE: str | None DEFAULT: None

background

TYPE: BackgroundTask | None DEFAULT: None

Source code in starlette/responses.py
175
176
177
178
179
180
181
182
183
def __init__(
    self,
    content: typing.Any,
    status_code: int = 200,
    headers: typing.Mapping[str, str] | None = None,
    media_type: str | None = None,
    background: BackgroundTask | None = None,
) -> None:
    super().__init__(content, status_code, headers, media_type, background)

charset class-attribute instance-attribute

charset = 'utf-8'

status_code instance-attribute

status_code = status_code

media_type class-attribute instance-attribute

media_type = 'application/json'

body instance-attribute

body = render(content)

background instance-attribute

background = background

headers property

headers

render

render(<span data-autorefs-optional="fastapi.responses.ORJSONResponse.render(content)">content</span>)
PARAMETER DESCRIPTION
content

TYPE: Any

Source code in fastapi/responses.py
44
45
46
47
48
def render(self, content: Any) -> bytes:
    assert orjson is not None, "orjson must be installed to use ORJSONResponse"
    return orjson.dumps(
        content, option=orjson.OPT_NON_STR_KEYS | orjson.OPT_SERIALIZE_NUMPY
    )

init_headers

init_headers(<span data-autorefs-optional="fastapi.responses.ORJSONResponse.init_headers(headers)">headers</span>=None)
PARAMETER DESCRIPTION
headers

TYPE: Mapping[str, str] | None DEFAULT: None

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
def init_headers(self, headers: typing.Mapping[str, str] | None = None) -> None:
    if headers is None:
        raw_headers: list[tuple[bytes, bytes]] = []
        populate_content_length = True
        populate_content_type = True
    else:
        raw_headers = [
            (k.lower().encode("latin-1"), v.encode("latin-1"))
            for k, v in headers.items()
        ]
        keys = [h[0] for h in raw_headers]
        populate_content_length = b"content-length" not in keys
        populate_content_type = b"content-type" not in keys

    body = getattr(self, "body", None)
    if (
        body is not None
        and populate_content_length
        and not (self.status_code < 200 or self.status_code in (204, 304))
    ):
        content_length = str(len(body))
        raw_headers.append((b"content-length", content_length.encode("latin-1")))

    content_type = self.media_type
    if content_type is not None and populate_content_type:
        if (
            content_type.startswith("text/")
            and "charset=" not in content_type.lower()
        ):
            content_type += "; charset=" + self.charset
        raw_headers.append((b"content-type", content_type.encode("latin-1")))

    self.raw_headers = raw_headers
set_cookie(<span data-autorefs-optional="fastapi.responses.ORJSONResponse.set_cookie(key)">key</span>, <span data-autorefs-optional="fastapi.responses.ORJSONResponse.set_cookie(value)">value</span>='', <span data-autorefs-optional="fastapi.responses.ORJSONResponse.set_cookie(max_age)">max_age</span>=None, <span data-autorefs-optional="fastapi.responses.ORJSONResponse.set_cookie(expires)">expires</span>=None, <span data-autorefs-optional="fastapi.responses.ORJSONResponse.set_cookie(path)">path</span>='/', <span data-autorefs-optional="fastapi.responses.ORJSONResponse.set_cookie(domain)">domain</span>=None, <span data-autorefs-optional="fastapi.responses.ORJSONResponse.set_cookie(secure)">secure</span>=False, <span data-autorefs-optional="fastapi.responses.ORJSONResponse.set_cookie(httponly)">httponly</span>=False, <span data-autorefs-optional="fastapi.responses.ORJSONResponse.set_cookie(samesite)">samesite</span>='lax')
PARAMETER DESCRIPTION
key

TYPE: str

value

TYPE: str DEFAULT: ''

max_age

TYPE: int | None DEFAULT: None

expires

TYPE: datetime | str | int | None DEFAULT: None

path

TYPE: str DEFAULT: '/'

domain

TYPE: str | None DEFAULT: None

secure

TYPE: bool DEFAULT: False

httponly

TYPE: bool DEFAULT: False

samesite

TYPE: Literal['lax', 'strict', 'none'] | None DEFAULT: 'lax'

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
def set_cookie(
    self,
    key: str,
    value: str = "",
    max_age: int | None = None,
    expires: datetime | str | int | None = None,
    path: str = "/",
    domain: str | None = None,
    secure: bool = False,
    httponly: bool = False,
    samesite: typing.Literal["lax", "strict", "none"] | None = "lax",
) -> None:
    cookie: http.cookies.BaseCookie[str] = http.cookies.SimpleCookie()
    cookie[key] = value
    if max_age is not None:
        cookie[key]["max-age"] = max_age
    if expires is not None:
        if isinstance(expires, datetime):
            cookie[key]["expires"] = format_datetime(expires, usegmt=True)
        else:
            cookie[key]["expires"] = expires
    if path is not None:
        cookie[key]["path"] = path
    if domain is not None:
        cookie[key]["domain"] = domain
    if secure:
        cookie[key]["secure"] = True
    if httponly:
        cookie[key]["httponly"] = True
    if samesite is not None:
        assert samesite.lower() in [
            "strict",
            "lax",
            "none",
        ], "samesite must be either 'strict', 'lax' or 'none'"
        cookie[key]["samesite"] = samesite
    cookie_val = cookie.output(header="").strip()
    self.raw_headers.append((b"set-cookie", cookie_val.encode("latin-1")))
delete_cookie(<span data-autorefs-optional="fastapi.responses.ORJSONResponse.delete_cookie(key)">key</span>, <span data-autorefs-optional="fastapi.responses.ORJSONResponse.delete_cookie(path)">path</span>='/', <span data-autorefs-optional="fastapi.responses.ORJSONResponse.delete_cookie(domain)">domain</span>=None, <span data-autorefs-optional="fastapi.responses.ORJSONResponse.delete_cookie(secure)">secure</span>=False, <span data-autorefs-optional="fastapi.responses.ORJSONResponse.delete_cookie(httponly)">httponly</span>=False, <span data-autorefs-optional="fastapi.responses.ORJSONResponse.delete_cookie(samesite)">samesite</span>='lax')
PARAMETER DESCRIPTION
key

TYPE: str

path

TYPE: str DEFAULT: '/'

domain

TYPE: str | None DEFAULT: None

secure

TYPE: bool DEFAULT: False

httponly

TYPE: bool DEFAULT: False

samesite

TYPE: Literal['lax', 'strict', 'none'] | None DEFAULT: 'lax'

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
def delete_cookie(
    self,
    key: str,
    path: str = "/",
    domain: str | None = None,
    secure: bool = False,
    httponly: bool = False,
    samesite: typing.Literal["lax", "strict", "none"] | None = "lax",
) -> None:
    self.set_cookie(
        key,
        max_age=0,
        expires=0,
        path=path,
        domain=domain,
        secure=secure,
        httponly=httponly,
        samesite=samesite,
    )

Starlette Responses

fastapi.responses.FileResponse

FileResponse(<span data-autorefs-optional="fastapi.responses.FileResponse(path)">path</span>, <span data-autorefs-optional="fastapi.responses.FileResponse(status_code)">status_code</span>=200, <span data-autorefs-optional="fastapi.responses.FileResponse(headers)">headers</span>=None, <span data-autorefs-optional="fastapi.responses.FileResponse(media_type)">media_type</span>=None, <span data-autorefs-optional="fastapi.responses.FileResponse(background)">background</span>=None, <span data-autorefs-optional="fastapi.responses.FileResponse(filename)">filename</span>=None, <span data-autorefs-optional="fastapi.responses.FileResponse(stat_result)">stat_result</span>=None, <span data-autorefs-optional="fastapi.responses.FileResponse(method)">method</span>=None, <span data-autorefs-optional="fastapi.responses.FileResponse(content_disposition_type)">content_disposition_type</span>='attachment')

Bases: Response

PARAMETER DESCRIPTION
path

TYPE: str | PathLike[str]

status_code

TYPE: int DEFAULT: 200

headers

TYPE: Mapping[str, str] | None DEFAULT: None

media_type

TYPE: str | None DEFAULT: None

background

TYPE: BackgroundTask | None DEFAULT: None

filename

TYPE: str | None DEFAULT: None

stat_result

TYPE: stat_result | None DEFAULT: None

method

TYPE: str | None DEFAULT: None

content_disposition_type

TYPE: str DEFAULT: 'attachment'

Source code in starlette/responses.py
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
301
302
303
304
305
306
307
308
309
310
311
def __init__(
    self,
    path: str | os.PathLike[str],
    status_code: int = 200,
    headers: typing.Mapping[str, str] | None = None,
    media_type: str | None = None,
    background: BackgroundTask | None = None,
    filename: str | None = None,
    stat_result: os.stat_result | None = None,
    method: str | None = None,
    content_disposition_type: str = "attachment",
) -> None:
    self.path = path
    self.status_code = status_code
    self.filename = filename
    if method is not None:
        warnings.warn(
            "The 'method' parameter is not used, and it will be removed.",
            DeprecationWarning,
        )
    if media_type is None:
        media_type = guess_type(filename or path)[0] or "text/plain"
    self.media_type = media_type
    self.background = background
    self.init_headers(headers)
    if self.filename is not None:
        content_disposition_filename = quote(self.filename)
        if content_disposition_filename != self.filename:
            content_disposition = "{}; filename*=utf-8''{}".format(
                content_disposition_type, content_disposition_filename
            )
        else:
            content_disposition = '{}; filename="{}"'.format(
                content_disposition_type, self.filename
            )
        self.headers.setdefault("content-disposition", content_disposition)
    self.stat_result = stat_result
    if stat_result is not None:
        self.set_stat_headers(stat_result)

chunk_size class-attribute instance-attribute

chunk_size = 64 * 1024

charset class-attribute instance-attribute

charset = 'utf-8'

status_code instance-attribute

status_code = status_code

media_type instance-attribute

media_type = media_type

body instance-attribute

body = render(content)

background instance-attribute

background = background

headers property

headers

render

render(<span data-autorefs-optional="fastapi.responses.FileResponse.render(content)">content</span>)
PARAMETER DESCRIPTION
content

TYPE: Any

Source code in starlette/responses.py
44
45
46
47
48
49
def render(self, content: typing.Any) -> bytes:
    if content is None:
        return b""
    if isinstance(content, bytes):
        return content
    return content.encode(self.charset)  # type: ignore

init_headers

init_headers(<span data-autorefs-optional="fastapi.responses.FileResponse.init_headers(headers)">headers</span>=None)
PARAMETER DESCRIPTION
headers

TYPE: Mapping[str, str] | None DEFAULT: None

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
def init_headers(self, headers: typing.Mapping[str, str] | None = None) -> None:
    if headers is None:
        raw_headers: list[tuple[bytes, bytes]] = []
        populate_content_length = True
        populate_content_type = True
    else:
        raw_headers = [
            (k.lower().encode("latin-1"), v.encode("latin-1"))
            for k, v in headers.items()
        ]
        keys = [h[0] for h in raw_headers]
        populate_content_length = b"content-length" not in keys
        populate_content_type = b"content-type" not in keys

    body = getattr(self, "body", None)
    if (
        body is not None
        and populate_content_length
        and not (self.status_code < 200 or self.status_code in (204, 304))
    ):
        content_length = str(len(body))
        raw_headers.append((b"content-length", content_length.encode("latin-1")))

    content_type = self.media_type
    if content_type is not None and populate_content_type:
        if (
            content_type.startswith("text/")
            and "charset=" not in content_type.lower()
        ):
            content_type += "; charset=" + self.charset
        raw_headers.append((b"content-type", content_type.encode("latin-1")))

    self.raw_headers = raw_headers
set_cookie(<span data-autorefs-optional="fastapi.responses.FileResponse.set_cookie(key)">key</span>, <span data-autorefs-optional="fastapi.responses.FileResponse.set_cookie(value)">value</span>='', <span data-autorefs-optional="fastapi.responses.FileResponse.set_cookie(max_age)">max_age</span>=None, <span data-autorefs-optional="fastapi.responses.FileResponse.set_cookie(expires)">expires</span>=None, <span data-autorefs-optional="fastapi.responses.FileResponse.set_cookie(path)">path</span>='/', <span data-autorefs-optional="fastapi.responses.FileResponse.set_cookie(domain)">domain</span>=None, <span data-autorefs-optional="fastapi.responses.FileResponse.set_cookie(secure)">secure</span>=False, <span data-autorefs-optional="fastapi.responses.FileResponse.set_cookie(httponly)">httponly</span>=False, <span data-autorefs-optional="fastapi.responses.FileResponse.set_cookie(samesite)">samesite</span>='lax')
PARAMETER DESCRIPTION
key

TYPE: str

value

TYPE: str DEFAULT: ''

max_age

TYPE: int | None DEFAULT: None

expires

TYPE: datetime | str | int | None DEFAULT: None

path

TYPE: str DEFAULT: '/'

domain

TYPE: str | None DEFAULT: None

secure

TYPE: bool DEFAULT: False

httponly

TYPE: bool DEFAULT: False

samesite

TYPE: Literal['lax', 'strict', 'none'] | None DEFAULT: 'lax'

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
def set_cookie(
    self,
    key: str,
    value: str = "",
    max_age: int | None = None,
    expires: datetime | str | int | None = None,
    path: str = "/",
    domain: str | None = None,
    secure: bool = False,
    httponly: bool = False,
    samesite: typing.Literal["lax", "strict", "none"] | None = "lax",
) -> None:
    cookie: http.cookies.BaseCookie[str] = http.cookies.SimpleCookie()
    cookie[key] = value
    if max_age is not None:
        cookie[key]["max-age"] = max_age
    if expires is not None:
        if isinstance(expires, datetime):
            cookie[key]["expires"] = format_datetime(expires, usegmt=True)
        else:
            cookie[key]["expires"] = expires
    if path is not None:
        cookie[key]["path"] = path
    if domain is not None:
        cookie[key]["domain"] = domain
    if secure:
        cookie[key]["secure"] = True
    if httponly:
        cookie[key]["httponly"] = True
    if samesite is not None:
        assert samesite.lower() in [
            "strict",
            "lax",
            "none",
        ], "samesite must be either 'strict', 'lax' or 'none'"
        cookie[key]["samesite"] = samesite
    cookie_val = cookie.output(header="").strip()
    self.raw_headers.append((b"set-cookie", cookie_val.encode("latin-1")))
delete_cookie(<span data-autorefs-optional="fastapi.responses.FileResponse.delete_cookie(key)">key</span>, <span data-autorefs-optional="fastapi.responses.FileResponse.delete_cookie(path)">path</span>='/', <span data-autorefs-optional="fastapi.responses.FileResponse.delete_cookie(domain)">domain</span>=None, <span data-autorefs-optional="fastapi.responses.FileResponse.delete_cookie(secure)">secure</span>=False, <span data-autorefs-optional="fastapi.responses.FileResponse.delete_cookie(httponly)">httponly</span>=False, <span data-autorefs-optional="fastapi.responses.FileResponse.delete_cookie(samesite)">samesite</span>='lax')
PARAMETER DESCRIPTION
key

TYPE: str

path

TYPE: str DEFAULT: '/'

domain

TYPE: str | None DEFAULT: None

secure

TYPE: bool DEFAULT: False

httponly

TYPE: bool DEFAULT: False

samesite

TYPE: Literal['lax', 'strict', 'none'] | None DEFAULT: 'lax'

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
def delete_cookie(
    self,
    key: str,
    path: str = "/",
    domain: str | None = None,
    secure: bool = False,
    httponly: bool = False,
    samesite: typing.Literal["lax", "strict", "none"] | None = "lax",
) -> None:
    self.set_cookie(
        key,
        max_age=0,
        expires=0,
        path=path,
        domain=domain,
        secure=secure,
        httponly=httponly,
        samesite=samesite,
    )

fastapi.responses.HTMLResponse

HTMLResponse(<span data-autorefs-optional="fastapi.responses.HTMLResponse(content)">content</span>=None, <span data-autorefs-optional="fastapi.responses.HTMLResponse(status_code)">status_code</span>=200, <span data-autorefs-optional="fastapi.responses.HTMLResponse(headers)">headers</span>=None, <span data-autorefs-optional="fastapi.responses.HTMLResponse(media_type)">media_type</span>=None, <span data-autorefs-optional="fastapi.responses.HTMLResponse(background)">background</span>=None)

Bases: Response

PARAMETER DESCRIPTION
content

TYPE: Any DEFAULT: None

status_code

TYPE: int DEFAULT: 200

headers

TYPE: Mapping[str, str] | None DEFAULT: None

media_type

TYPE: str | None DEFAULT: None

background

TYPE: BackgroundTask | None DEFAULT: None

Source code in starlette/responses.py
29
30
31
32
33
34
35
36
37
38
39
40
41
42
def __init__(
    self,
    content: typing.Any = None,
    status_code: int = 200,
    headers: typing.Mapping[str, str] | None = None,
    media_type: str | None = None,
    background: BackgroundTask | None = None,
) -> None:
    self.status_code = status_code
    if media_type is not None:
        self.media_type = media_type
    self.background = background
    self.body = self.render(content)
    self.init_headers(headers)

charset class-attribute instance-attribute

charset = 'utf-8'

status_code instance-attribute

status_code = status_code

media_type class-attribute instance-attribute

media_type = 'text/html'

body instance-attribute

body = render(content)

background instance-attribute

background = background

headers property

headers

render

render(<span data-autorefs-optional="fastapi.responses.HTMLResponse.render(content)">content</span>)
PARAMETER DESCRIPTION
content

TYPE: Any

Source code in starlette/responses.py
44
45
46
47
48
49
def render(self, content: typing.Any) -> bytes:
    if content is None:
        return b""
    if isinstance(content, bytes):
        return content
    return content.encode(self.charset)  # type: ignore

init_headers

init_headers(<span data-autorefs-optional="fastapi.responses.HTMLResponse.init_headers(headers)">headers</span>=None)
PARAMETER DESCRIPTION
headers

TYPE: Mapping[str, str] | None DEFAULT: None

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
def init_headers(self, headers: typing.Mapping[str, str] | None = None) -> None:
    if headers is None:
        raw_headers: list[tuple[bytes, bytes]] = []
        populate_content_length = True
        populate_content_type = True
    else:
        raw_headers = [
            (k.lower().encode("latin-1"), v.encode("latin-1"))
            for k, v in headers.items()
        ]
        keys = [h[0] for h in raw_headers]
        populate_content_length = b"content-length" not in keys
        populate_content_type = b"content-type" not in keys

    body = getattr(self, "body", None)
    if (
        body is not None
        and populate_content_length
        and not (self.status_code < 200 or self.status_code in (204, 304))
    ):
        content_length = str(len(body))
        raw_headers.append((b"content-length", content_length.encode("latin-1")))

    content_type = self.media_type
    if content_type is not None and populate_content_type:
        if (
            content_type.startswith("text/")
            and "charset=" not in content_type.lower()
        ):
            content_type += "; charset=" + self.charset
        raw_headers.append((b"content-type", content_type.encode("latin-1")))

    self.raw_headers = raw_headers
set_cookie(<span data-autorefs-optional="fastapi.responses.HTMLResponse.set_cookie(key)">key</span>, <span data-autorefs-optional="fastapi.responses.HTMLResponse.set_cookie(value)">value</span>='', <span data-autorefs-optional="fastapi.responses.HTMLResponse.set_cookie(max_age)">max_age</span>=None, <span data-autorefs-optional="fastapi.responses.HTMLResponse.set_cookie(expires)">expires</span>=None, <span data-autorefs-optional="fastapi.responses.HTMLResponse.set_cookie(path)">path</span>='/', <span data-autorefs-optional="fastapi.responses.HTMLResponse.set_cookie(domain)">domain</span>=None, <span data-autorefs-optional="fastapi.responses.HTMLResponse.set_cookie(secure)">secure</span>=False, <span data-autorefs-optional="fastapi.responses.HTMLResponse.set_cookie(httponly)">httponly</span>=False, <span data-autorefs-optional="fastapi.responses.HTMLResponse.set_cookie(samesite)">samesite</span>='lax')
PARAMETER DESCRIPTION
key

TYPE: str

value

TYPE: str DEFAULT: ''

max_age

TYPE: int | None DEFAULT: None

expires

TYPE: datetime | str | int | None DEFAULT: None

path

TYPE: str DEFAULT: '/'

domain

TYPE: str | None DEFAULT: None

secure

TYPE: bool DEFAULT: False

httponly

TYPE: bool DEFAULT: False

samesite

TYPE: Literal['lax', 'strict', 'none'] | None DEFAULT: 'lax'

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
def set_cookie(
    self,
    key: str,
    value: str = "",
    max_age: int | None = None,
    expires: datetime | str | int | None = None,
    path: str = "/",
    domain: str | None = None,
    secure: bool = False,
    httponly: bool = False,
    samesite: typing.Literal["lax", "strict", "none"] | None = "lax",
) -> None:
    cookie: http.cookies.BaseCookie[str] = http.cookies.SimpleCookie()
    cookie[key] = value
    if max_age is not None:
        cookie[key]["max-age"] = max_age
    if expires is not None:
        if isinstance(expires, datetime):
            cookie[key]["expires"] = format_datetime(expires, usegmt=True)
        else:
            cookie[key]["expires"] = expires
    if path is not None:
        cookie[key]["path"] = path
    if domain is not None:
        cookie[key]["domain"] = domain
    if secure:
        cookie[key]["secure"] = True
    if httponly:
        cookie[key]["httponly"] = True
    if samesite is not None:
        assert samesite.lower() in [
            "strict",
            "lax",
            "none",
        ], "samesite must be either 'strict', 'lax' or 'none'"
        cookie[key]["samesite"] = samesite
    cookie_val = cookie.output(header="").strip()
    self.raw_headers.append((b"set-cookie", cookie_val.encode("latin-1")))
delete_cookie(<span data-autorefs-optional="fastapi.responses.HTMLResponse.delete_cookie(key)">key</span>, <span data-autorefs-optional="fastapi.responses.HTMLResponse.delete_cookie(path)">path</span>='/', <span data-autorefs-optional="fastapi.responses.HTMLResponse.delete_cookie(domain)">domain</span>=None, <span data-autorefs-optional="fastapi.responses.HTMLResponse.delete_cookie(secure)">secure</span>=False, <span data-autorefs-optional="fastapi.responses.HTMLResponse.delete_cookie(httponly)">httponly</span>=False, <span data-autorefs-optional="fastapi.responses.HTMLResponse.delete_cookie(samesite)">samesite</span>='lax')
PARAMETER DESCRIPTION
key

TYPE: str

path

TYPE: str DEFAULT: '/'

domain

TYPE: str | None DEFAULT: None

secure

TYPE: bool DEFAULT: False

httponly

TYPE: bool DEFAULT: False

samesite

TYPE: Literal['lax', 'strict', 'none'] | None DEFAULT: 'lax'

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
def delete_cookie(
    self,
    key: str,
    path: str = "/",
    domain: str | None = None,
    secure: bool = False,
    httponly: bool = False,
    samesite: typing.Literal["lax", "strict", "none"] | None = "lax",
) -> None:
    self.set_cookie(
        key,
        max_age=0,
        expires=0,
        path=path,
        domain=domain,
        secure=secure,
        httponly=httponly,
        samesite=samesite,
    )

fastapi.responses.JSONResponse

JSONResponse(<span data-autorefs-optional="fastapi.responses.JSONResponse(content)">content</span>, <span data-autorefs-optional="fastapi.responses.JSONResponse(status_code)">status_code</span>=200, <span data-autorefs-optional="fastapi.responses.JSONResponse(headers)">headers</span>=None, <span data-autorefs-optional="fastapi.responses.JSONResponse(media_type)">media_type</span>=None, <span data-autorefs-optional="fastapi.responses.JSONResponse(background)">background</span>=None)

Bases: Response

PARAMETER DESCRIPTION
content

TYPE: Any

status_code

TYPE: int DEFAULT: 200

headers

TYPE: Mapping[str, str] | None DEFAULT: None

media_type

TYPE: str | None DEFAULT: None

background

TYPE: BackgroundTask | None DEFAULT: None

Source code in starlette/responses.py
175
176
177
178
179
180
181
182
183
def __init__(
    self,
    content: typing.Any,
    status_code: int = 200,
    headers: typing.Mapping[str, str] | None = None,
    media_type: str | None = None,
    background: BackgroundTask | None = None,
) -> None:
    super().__init__(content, status_code, headers, media_type, background)

charset class-attribute instance-attribute

charset = 'utf-8'

status_code instance-attribute

status_code = status_code

media_type class-attribute instance-attribute

media_type = 'application/json'

body instance-attribute

body = render(content)

background instance-attribute

background = background

headers property

headers

render

render(<span data-autorefs-optional="fastapi.responses.JSONResponse.render(content)">content</span>)
PARAMETER DESCRIPTION
content

TYPE: Any

Source code in starlette/responses.py
185
186
187
188
189
190
191
192
def render(self, content: typing.Any) -> bytes:
    return json.dumps(
        content,
        ensure_ascii=False,
        allow_nan=False,
        indent=None,
        separators=(",", ":"),
    ).encode("utf-8")

init_headers

init_headers(<span data-autorefs-optional="fastapi.responses.JSONResponse.init_headers(headers)">headers</span>=None)
PARAMETER DESCRIPTION
headers

TYPE: Mapping[str, str] | None DEFAULT: None

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
def init_headers(self, headers: typing.Mapping[str, str] | None = None) -> None:
    if headers is None:
        raw_headers: list[tuple[bytes, bytes]] = []
        populate_content_length = True
        populate_content_type = True
    else:
        raw_headers = [
            (k.lower().encode("latin-1"), v.encode("latin-1"))
            for k, v in headers.items()
        ]
        keys = [h[0] for h in raw_headers]
        populate_content_length = b"content-length" not in keys
        populate_content_type = b"content-type" not in keys

    body = getattr(self, "body", None)
    if (
        body is not None
        and populate_content_length
        and not (self.status_code < 200 or self.status_code in (204, 304))
    ):
        content_length = str(len(body))
        raw_headers.append((b"content-length", content_length.encode("latin-1")))

    content_type = self.media_type
    if content_type is not None and populate_content_type:
        if (
            content_type.startswith("text/")
            and "charset=" not in content_type.lower()
        ):
            content_type += "; charset=" + self.charset
        raw_headers.append((b"content-type", content_type.encode("latin-1")))

    self.raw_headers = raw_headers
set_cookie(<span data-autorefs-optional="fastapi.responses.JSONResponse.set_cookie(key)">key</span>, <span data-autorefs-optional="fastapi.responses.JSONResponse.set_cookie(value)">value</span>='', <span data-autorefs-optional="fastapi.responses.JSONResponse.set_cookie(max_age)">max_age</span>=None, <span data-autorefs-optional="fastapi.responses.JSONResponse.set_cookie(expires)">expires</span>=None, <span data-autorefs-optional="fastapi.responses.JSONResponse.set_cookie(path)">path</span>='/', <span data-autorefs-optional="fastapi.responses.JSONResponse.set_cookie(domain)">domain</span>=None, <span data-autorefs-optional="fastapi.responses.JSONResponse.set_cookie(secure)">secure</span>=False, <span data-autorefs-optional="fastapi.responses.JSONResponse.set_cookie(httponly)">httponly</span>=False, <span data-autorefs-optional="fastapi.responses.JSONResponse.set_cookie(samesite)">samesite</span>='lax')
PARAMETER DESCRIPTION
key

TYPE: str

value

TYPE: str DEFAULT: ''

max_age

TYPE: int | None DEFAULT: None

expires

TYPE: datetime | str | int | None DEFAULT: None

path

TYPE: str DEFAULT: '/'

domain

TYPE: str | None DEFAULT: None

secure

TYPE: bool DEFAULT: False

httponly

TYPE: bool DEFAULT: False

samesite

TYPE: Literal['lax', 'strict', 'none'] | None DEFAULT: 'lax'

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
def set_cookie(
    self,
    key: str,
    value: str = "",
    max_age: int | None = None,
    expires: datetime | str | int | None = None,
    path: str = "/",
    domain: str | None = None,
    secure: bool = False,
    httponly: bool = False,
    samesite: typing.Literal["lax", "strict", "none"] | None = "lax",
) -> None:
    cookie: http.cookies.BaseCookie[str] = http.cookies.SimpleCookie()
    cookie[key] = value
    if max_age is not None:
        cookie[key]["max-age"] = max_age
    if expires is not None:
        if isinstance(expires, datetime):
            cookie[key]["expires"] = format_datetime(expires, usegmt=True)
        else:
            cookie[key]["expires"] = expires
    if path is not None:
        cookie[key]["path"] = path
    if domain is not None:
        cookie[key]["domain"] = domain
    if secure:
        cookie[key]["secure"] = True
    if httponly:
        cookie[key]["httponly"] = True
    if samesite is not None:
        assert samesite.lower() in [
            "strict",
            "lax",
            "none",
        ], "samesite must be either 'strict', 'lax' or 'none'"
        cookie[key]["samesite"] = samesite
    cookie_val = cookie.output(header="").strip()
    self.raw_headers.append((b"set-cookie", cookie_val.encode("latin-1")))
delete_cookie(<span data-autorefs-optional="fastapi.responses.JSONResponse.delete_cookie(key)">key</span>, <span data-autorefs-optional="fastapi.responses.JSONResponse.delete_cookie(path)">path</span>='/', <span data-autorefs-optional="fastapi.responses.JSONResponse.delete_cookie(domain)">domain</span>=None, <span data-autorefs-optional="fastapi.responses.JSONResponse.delete_cookie(secure)">secure</span>=False, <span data-autorefs-optional="fastapi.responses.JSONResponse.delete_cookie(httponly)">httponly</span>=False, <span data-autorefs-optional="fastapi.responses.JSONResponse.delete_cookie(samesite)">samesite</span>='lax')
PARAMETER DESCRIPTION
key

TYPE: str

path

TYPE: str DEFAULT: '/'

domain

TYPE: str | None DEFAULT: None

secure

TYPE: bool DEFAULT: False

httponly

TYPE: bool DEFAULT: False

samesite

TYPE: Literal['lax', 'strict', 'none'] | None DEFAULT: 'lax'

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
def delete_cookie(
    self,
    key: str,
    path: str = "/",
    domain: str | None = None,
    secure: bool = False,
    httponly: bool = False,
    samesite: typing.Literal["lax", "strict", "none"] | None = "lax",
) -> None:
    self.set_cookie(
        key,
        max_age=0,
        expires=0,
        path=path,
        domain=domain,
        secure=secure,
        httponly=httponly,
        samesite=samesite,
    )

fastapi.responses.PlainTextResponse

PlainTextResponse(<span data-autorefs-optional="fastapi.responses.PlainTextResponse(content)">content</span>=None, <span data-autorefs-optional="fastapi.responses.PlainTextResponse(status_code)">status_code</span>=200, <span data-autorefs-optional="fastapi.responses.PlainTextResponse(headers)">headers</span>=None, <span data-autorefs-optional="fastapi.responses.PlainTextResponse(media_type)">media_type</span>=None, <span data-autorefs-optional="fastapi.responses.PlainTextResponse(background)">background</span>=None)

Bases: Response

PARAMETER DESCRIPTION
content

TYPE: Any DEFAULT: None

status_code

TYPE: int DEFAULT: 200

headers

TYPE: Mapping[str, str] | None DEFAULT: None

media_type

TYPE: str | None DEFAULT: None

background

TYPE: BackgroundTask | None DEFAULT: None

Source code in starlette/responses.py
29
30
31
32
33
34
35
36
37
38
39
40
41
42
def __init__(
    self,
    content: typing.Any = None,
    status_code: int = 200,
    headers: typing.Mapping[str, str] | None = None,
    media_type: str | None = None,
    background: BackgroundTask | None = None,
) -> None:
    self.status_code = status_code
    if media_type is not None:
        self.media_type = media_type
    self.background = background
    self.body = self.render(content)
    self.init_headers(headers)

charset class-attribute instance-attribute

charset = 'utf-8'

status_code instance-attribute

status_code = status_code

media_type class-attribute instance-attribute

media_type = 'text/plain'

body instance-attribute

body = render(content)

background instance-attribute

background = background

headers property

headers

render

render(<span data-autorefs-optional="fastapi.responses.PlainTextResponse.render(content)">content</span>)
PARAMETER DESCRIPTION
content

TYPE: Any

Source code in starlette/responses.py
44
45
46
47
48
49
def render(self, content: typing.Any) -> bytes:
    if content is None:
        return b""
    if isinstance(content, bytes):
        return content
    return content.encode(self.charset)  # type: ignore

init_headers

init_headers(<span data-autorefs-optional="fastapi.responses.PlainTextResponse.init_headers(headers)">headers</span>=None)
PARAMETER DESCRIPTION
headers

TYPE: Mapping[str, str] | None DEFAULT: None

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
def init_headers(self, headers: typing.Mapping[str, str] | None = None) -> None:
    if headers is None:
        raw_headers: list[tuple[bytes, bytes]] = []
        populate_content_length = True
        populate_content_type = True
    else:
        raw_headers = [
            (k.lower().encode("latin-1"), v.encode("latin-1"))
            for k, v in headers.items()
        ]
        keys = [h[0] for h in raw_headers]
        populate_content_length = b"content-length" not in keys
        populate_content_type = b"content-type" not in keys

    body = getattr(self, "body", None)
    if (
        body is not None
        and populate_content_length
        and not (self.status_code < 200 or self.status_code in (204, 304))
    ):
        content_length = str(len(body))
        raw_headers.append((b"content-length", content_length.encode("latin-1")))

    content_type = self.media_type
    if content_type is not None and populate_content_type:
        if (
            content_type.startswith("text/")
            and "charset=" not in content_type.lower()
        ):
            content_type += "; charset=" + self.charset
        raw_headers.append((b"content-type", content_type.encode("latin-1")))

    self.raw_headers = raw_headers
set_cookie(<span data-autorefs-optional="fastapi.responses.PlainTextResponse.set_cookie(key)">key</span>, <span data-autorefs-optional="fastapi.responses.PlainTextResponse.set_cookie(value)">value</span>='', <span data-autorefs-optional="fastapi.responses.PlainTextResponse.set_cookie(max_age)">max_age</span>=None, <span data-autorefs-optional="fastapi.responses.PlainTextResponse.set_cookie(expires)">expires</span>=None, <span data-autorefs-optional="fastapi.responses.PlainTextResponse.set_cookie(path)">path</span>='/', <span data-autorefs-optional="fastapi.responses.PlainTextResponse.set_cookie(domain)">domain</span>=None, <span data-autorefs-optional="fastapi.responses.PlainTextResponse.set_cookie(secure)">secure</span>=False, <span data-autorefs-optional="fastapi.responses.PlainTextResponse.set_cookie(httponly)">httponly</span>=False, <span data-autorefs-optional="fastapi.responses.PlainTextResponse.set_cookie(samesite)">samesite</span>='lax')
PARAMETER DESCRIPTION
key

TYPE: str

value

TYPE: str DEFAULT: ''

max_age

TYPE: int | None DEFAULT: None

expires

TYPE: datetime | str | int | None DEFAULT: None

path

TYPE: str DEFAULT: '/'

domain

TYPE: str | None DEFAULT: None

secure

TYPE: bool DEFAULT: False

httponly

TYPE: bool DEFAULT: False

samesite

TYPE: Literal['lax', 'strict', 'none'] | None DEFAULT: 'lax'

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
def set_cookie(
    self,
    key: str,
    value: str = "",
    max_age: int | None = None,
    expires: datetime | str | int | None = None,
    path: str = "/",
    domain: str | None = None,
    secure: bool = False,
    httponly: bool = False,
    samesite: typing.Literal["lax", "strict", "none"] | None = "lax",
) -> None:
    cookie: http.cookies.BaseCookie[str] = http.cookies.SimpleCookie()
    cookie[key] = value
    if max_age is not None:
        cookie[key]["max-age"] = max_age
    if expires is not None:
        if isinstance(expires, datetime):
            cookie[key]["expires"] = format_datetime(expires, usegmt=True)
        else:
            cookie[key]["expires"] = expires
    if path is not None:
        cookie[key]["path"] = path
    if domain is not None:
        cookie[key]["domain"] = domain
    if secure:
        cookie[key]["secure"] = True
    if httponly:
        cookie[key]["httponly"] = True
    if samesite is not None:
        assert samesite.lower() in [
            "strict",
            "lax",
            "none",
        ], "samesite must be either 'strict', 'lax' or 'none'"
        cookie[key]["samesite"] = samesite
    cookie_val = cookie.output(header="").strip()
    self.raw_headers.append((b"set-cookie", cookie_val.encode("latin-1")))
delete_cookie(<span data-autorefs-optional="fastapi.responses.PlainTextResponse.delete_cookie(key)">key</span>, <span data-autorefs-optional="fastapi.responses.PlainTextResponse.delete_cookie(path)">path</span>='/', <span data-autorefs-optional="fastapi.responses.PlainTextResponse.delete_cookie(domain)">domain</span>=None, <span data-autorefs-optional="fastapi.responses.PlainTextResponse.delete_cookie(secure)">secure</span>=False, <span data-autorefs-optional="fastapi.responses.PlainTextResponse.delete_cookie(httponly)">httponly</span>=False, <span data-autorefs-optional="fastapi.responses.PlainTextResponse.delete_cookie(samesite)">samesite</span>='lax')
PARAMETER DESCRIPTION
key

TYPE: str

path

TYPE: str DEFAULT: '/'

domain

TYPE: str | None DEFAULT: None

secure

TYPE: bool DEFAULT: False

httponly

TYPE: bool DEFAULT: False

samesite

TYPE: Literal['lax', 'strict', 'none'] | None DEFAULT: 'lax'

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
def delete_cookie(
    self,
    key: str,
    path: str = "/",
    domain: str | None = None,
    secure: bool = False,
    httponly: bool = False,
    samesite: typing.Literal["lax", "strict", "none"] | None = "lax",
) -> None:
    self.set_cookie(
        key,
        max_age=0,
        expires=0,
        path=path,
        domain=domain,
        secure=secure,
        httponly=httponly,
        samesite=samesite,
    )

fastapi.responses.RedirectResponse

RedirectResponse(<span data-autorefs-optional="fastapi.responses.RedirectResponse(url)">url</span>, <span data-autorefs-optional="fastapi.responses.RedirectResponse(status_code)">status_code</span>=307, <span data-autorefs-optional="fastapi.responses.RedirectResponse(headers)">headers</span>=None, <span data-autorefs-optional="fastapi.responses.RedirectResponse(background)">background</span>=None)

Bases: Response

PARAMETER DESCRIPTION
url

TYPE: str | URL

status_code

TYPE: int DEFAULT: 307

headers

TYPE: Mapping[str, str] | None DEFAULT: None

background

TYPE: BackgroundTask | None DEFAULT: None

Source code in starlette/responses.py
196
197
198
199
200
201
202
203
204
205
206
def __init__(
    self,
    url: str | URL,
    status_code: int = 307,
    headers: typing.Mapping[str, str] | None = None,
    background: BackgroundTask | None = None,
) -> None:
    super().__init__(
        content=b"", status_code=status_code, headers=headers, background=background
    )
    self.headers["location"] = quote(str(url), safe=":/%#?=@[]!$&'()*+,;")

charset class-attribute instance-attribute

charset = 'utf-8'

status_code instance-attribute

status_code = status_code

media_type class-attribute instance-attribute

media_type = None

body instance-attribute

body = render(content)

background instance-attribute

background = background

headers property

headers

render

render(<span data-autorefs-optional="fastapi.responses.RedirectResponse.render(content)">content</span>)
PARAMETER DESCRIPTION
content

TYPE: Any

Source code in starlette/responses.py
44
45
46
47
48
49
def render(self, content: typing.Any) -> bytes:
    if content is None:
        return b""
    if isinstance(content, bytes):
        return content
    return content.encode(self.charset)  # type: ignore

init_headers

init_headers(<span data-autorefs-optional="fastapi.responses.RedirectResponse.init_headers(headers)">headers</span>=None)
PARAMETER DESCRIPTION
headers

TYPE: Mapping[str, str] | None DEFAULT: None

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
def init_headers(self, headers: typing.Mapping[str, str] | None = None) -> None:
    if headers is None:
        raw_headers: list[tuple[bytes, bytes]] = []
        populate_content_length = True
        populate_content_type = True
    else:
        raw_headers = [
            (k.lower().encode("latin-1"), v.encode("latin-1"))
            for k, v in headers.items()
        ]
        keys = [h[0] for h in raw_headers]
        populate_content_length = b"content-length" not in keys
        populate_content_type = b"content-type" not in keys

    body = getattr(self, "body", None)
    if (
        body is not None
        and populate_content_length
        and not (self.status_code < 200 or self.status_code in (204, 304))
    ):
        content_length = str(len(body))
        raw_headers.append((b"content-length", content_length.encode("latin-1")))

    content_type = self.media_type
    if content_type is not None and populate_content_type:
        if (
            content_type.startswith("text/")
            and "charset=" not in content_type.lower()
        ):
            content_type += "; charset=" + self.charset
        raw_headers.append((b"content-type", content_type.encode("latin-1")))

    self.raw_headers = raw_headers
set_cookie(<span data-autorefs-optional="fastapi.responses.RedirectResponse.set_cookie(key)">key</span>, <span data-autorefs-optional="fastapi.responses.RedirectResponse.set_cookie(value)">value</span>='', <span data-autorefs-optional="fastapi.responses.RedirectResponse.set_cookie(max_age)">max_age</span>=None, <span data-autorefs-optional="fastapi.responses.RedirectResponse.set_cookie(expires)">expires</span>=None, <span data-autorefs-optional="fastapi.responses.RedirectResponse.set_cookie(path)">path</span>='/', <span data-autorefs-optional="fastapi.responses.RedirectResponse.set_cookie(domain)">domain</span>=None, <span data-autorefs-optional="fastapi.responses.RedirectResponse.set_cookie(secure)">secure</span>=False, <span data-autorefs-optional="fastapi.responses.RedirectResponse.set_cookie(httponly)">httponly</span>=False, <span data-autorefs-optional="fastapi.responses.RedirectResponse.set_cookie(samesite)">samesite</span>='lax')
PARAMETER DESCRIPTION
key

TYPE: str

value

TYPE: str DEFAULT: ''

max_age

TYPE: int | None DEFAULT: None

expires

TYPE: datetime | str | int | None DEFAULT: None

path

TYPE: str DEFAULT: '/'

domain

TYPE: str | None DEFAULT: None

secure

TYPE: bool DEFAULT: False

httponly

TYPE: bool DEFAULT: False

samesite

TYPE: Literal['lax', 'strict', 'none'] | None DEFAULT: 'lax'

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
def set_cookie(
    self,
    key: str,
    value: str = "",
    max_age: int | None = None,
    expires: datetime | str | int | None = None,
    path: str = "/",
    domain: str | None = None,
    secure: bool = False,
    httponly: bool = False,
    samesite: typing.Literal["lax", "strict", "none"] | None = "lax",
) -> None:
    cookie: http.cookies.BaseCookie[str] = http.cookies.SimpleCookie()
    cookie[key] = value
    if max_age is not None:
        cookie[key]["max-age"] = max_age
    if expires is not None:
        if isinstance(expires, datetime):
            cookie[key]["expires"] = format_datetime(expires, usegmt=True)
        else:
            cookie[key]["expires"] = expires
    if path is not None:
        cookie[key]["path"] = path
    if domain is not None:
        cookie[key]["domain"] = domain
    if secure:
        cookie[key]["secure"] = True
    if httponly:
        cookie[key]["httponly"] = True
    if samesite is not None:
        assert samesite.lower() in [
            "strict",
            "lax",
            "none",
        ], "samesite must be either 'strict', 'lax' or 'none'"
        cookie[key]["samesite"] = samesite
    cookie_val = cookie.output(header="").strip()
    self.raw_headers.append((b"set-cookie", cookie_val.encode("latin-1")))
delete_cookie(<span data-autorefs-optional="fastapi.responses.RedirectResponse.delete_cookie(key)">key</span>, <span data-autorefs-optional="fastapi.responses.RedirectResponse.delete_cookie(path)">path</span>='/', <span data-autorefs-optional="fastapi.responses.RedirectResponse.delete_cookie(domain)">domain</span>=None, <span data-autorefs-optional="fastapi.responses.RedirectResponse.delete_cookie(secure)">secure</span>=False, <span data-autorefs-optional="fastapi.responses.RedirectResponse.delete_cookie(httponly)">httponly</span>=False, <span data-autorefs-optional="fastapi.responses.RedirectResponse.delete_cookie(samesite)">samesite</span>='lax')
PARAMETER DESCRIPTION
key

TYPE: str

path

TYPE: str DEFAULT: '/'

domain

TYPE: str | None DEFAULT: None

secure

TYPE: bool DEFAULT: False

httponly

TYPE: bool DEFAULT: False

samesite

TYPE: Literal['lax', 'strict', 'none'] | None DEFAULT: 'lax'

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
def delete_cookie(
    self,
    key: str,
    path: str = "/",
    domain: str | None = None,
    secure: bool = False,
    httponly: bool = False,
    samesite: typing.Literal["lax", "strict", "none"] | None = "lax",
) -> None:
    self.set_cookie(
        key,
        max_age=0,
        expires=0,
        path=path,
        domain=domain,
        secure=secure,
        httponly=httponly,
        samesite=samesite,
    )

fastapi.responses.Response

Response(<span data-autorefs-optional="fastapi.responses.Response(content)">content</span>=None, <span data-autorefs-optional="fastapi.responses.Response(status_code)">status_code</span>=200, <span data-autorefs-optional="fastapi.responses.Response(headers)">headers</span>=None, <span data-autorefs-optional="fastapi.responses.Response(media_type)">media_type</span>=None, <span data-autorefs-optional="fastapi.responses.Response(background)">background</span>=None)
PARAMETER DESCRIPTION
content

TYPE: Any DEFAULT: None

status_code

TYPE: int DEFAULT: 200

headers

TYPE: Mapping[str, str] | None DEFAULT: None

media_type

TYPE: str | None DEFAULT: None

background

TYPE: BackgroundTask | None DEFAULT: None

Source code in starlette/responses.py
29
30
31
32
33
34
35
36
37
38
39
40
41
42
def __init__(
    self,
    content: typing.Any = None,
    status_code: int = 200,
    headers: typing.Mapping[str, str] | None = None,
    media_type: str | None = None,
    background: BackgroundTask | None = None,
) -> None:
    self.status_code = status_code
    if media_type is not None:
        self.media_type = media_type
    self.background = background
    self.body = self.render(content)
    self.init_headers(headers)

charset class-attribute instance-attribute

charset = 'utf-8'

status_code instance-attribute

status_code = status_code

media_type class-attribute instance-attribute

media_type = None

body instance-attribute

body = render(content)

background instance-attribute

background = background

headers property

headers

render

render(<span data-autorefs-optional="fastapi.responses.Response.render(content)">content</span>)
PARAMETER DESCRIPTION
content

TYPE: Any

Source code in starlette/responses.py
44
45
46
47
48
49
def render(self, content: typing.Any) -> bytes:
    if content is None:
        return b""
    if isinstance(content, bytes):
        return content
    return content.encode(self.charset)  # type: ignore

init_headers

init_headers(<span data-autorefs-optional="fastapi.responses.Response.init_headers(headers)">headers</span>=None)
PARAMETER DESCRIPTION
headers

TYPE: Mapping[str, str] | None DEFAULT: None

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
def init_headers(self, headers: typing.Mapping[str, str] | None = None) -> None:
    if headers is None:
        raw_headers: list[tuple[bytes, bytes]] = []
        populate_content_length = True
        populate_content_type = True
    else:
        raw_headers = [
            (k.lower().encode("latin-1"), v.encode("latin-1"))
            for k, v in headers.items()
        ]
        keys = [h[0] for h in raw_headers]
        populate_content_length = b"content-length" not in keys
        populate_content_type = b"content-type" not in keys

    body = getattr(self, "body", None)
    if (
        body is not None
        and populate_content_length
        and not (self.status_code < 200 or self.status_code in (204, 304))
    ):
        content_length = str(len(body))
        raw_headers.append((b"content-length", content_length.encode("latin-1")))

    content_type = self.media_type
    if content_type is not None and populate_content_type:
        if (
            content_type.startswith("text/")
            and "charset=" not in content_type.lower()
        ):
            content_type += "; charset=" + self.charset
        raw_headers.append((b"content-type", content_type.encode("latin-1")))

    self.raw_headers = raw_headers
set_cookie(<span data-autorefs-optional="fastapi.responses.Response.set_cookie(key)">key</span>, <span data-autorefs-optional="fastapi.responses.Response.set_cookie(value)">value</span>='', <span data-autorefs-optional="fastapi.responses.Response.set_cookie(max_age)">max_age</span>=None, <span data-autorefs-optional="fastapi.responses.Response.set_cookie(expires)">expires</span>=None, <span data-autorefs-optional="fastapi.responses.Response.set_cookie(path)">path</span>='/', <span data-autorefs-optional="fastapi.responses.Response.set_cookie(domain)">domain</span>=None, <span data-autorefs-optional="fastapi.responses.Response.set_cookie(secure)">secure</span>=False, <span data-autorefs-optional="fastapi.responses.Response.set_cookie(httponly)">httponly</span>=False, <span data-autorefs-optional="fastapi.responses.Response.set_cookie(samesite)">samesite</span>='lax')
PARAMETER DESCRIPTION
key

TYPE: str

value

TYPE: str DEFAULT: ''

max_age

TYPE: int | None DEFAULT: None

expires

TYPE: datetime | str | int | None DEFAULT: None

path

TYPE: str DEFAULT: '/'

domain

TYPE: str | None DEFAULT: None

secure

TYPE: bool DEFAULT: False

httponly

TYPE: bool DEFAULT: False

samesite

TYPE: Literal['lax', 'strict', 'none'] | None DEFAULT: 'lax'

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
def set_cookie(
    self,
    key: str,
    value: str = "",
    max_age: int | None = None,
    expires: datetime | str | int | None = None,
    path: str = "/",
    domain: str | None = None,
    secure: bool = False,
    httponly: bool = False,
    samesite: typing.Literal["lax", "strict", "none"] | None = "lax",
) -> None:
    cookie: http.cookies.BaseCookie[str] = http.cookies.SimpleCookie()
    cookie[key] = value
    if max_age is not None:
        cookie[key]["max-age"] = max_age
    if expires is not None:
        if isinstance(expires, datetime):
            cookie[key]["expires"] = format_datetime(expires, usegmt=True)
        else:
            cookie[key]["expires"] = expires
    if path is not None:
        cookie[key]["path"] = path
    if domain is not None:
        cookie[key]["domain"] = domain
    if secure:
        cookie[key]["secure"] = True
    if httponly:
        cookie[key]["httponly"] = True
    if samesite is not None:
        assert samesite.lower() in [
            "strict",
            "lax",
            "none",
        ], "samesite must be either 'strict', 'lax' or 'none'"
        cookie[key]["samesite"] = samesite
    cookie_val = cookie.output(header="").strip()
    self.raw_headers.append((b"set-cookie", cookie_val.encode("latin-1")))
delete_cookie(<span data-autorefs-optional="fastapi.responses.Response.delete_cookie(key)">key</span>, <span data-autorefs-optional="fastapi.responses.Response.delete_cookie(path)">path</span>='/', <span data-autorefs-optional="fastapi.responses.Response.delete_cookie(domain)">domain</span>=None, <span data-autorefs-optional="fastapi.responses.Response.delete_cookie(secure)">secure</span>=False, <span data-autorefs-optional="fastapi.responses.Response.delete_cookie(httponly)">httponly</span>=False, <span data-autorefs-optional="fastapi.responses.Response.delete_cookie(samesite)">samesite</span>='lax')
PARAMETER DESCRIPTION
key

TYPE: str

path

TYPE: str DEFAULT: '/'

domain

TYPE: str | None DEFAULT: None

secure

TYPE: bool DEFAULT: False

httponly

TYPE: bool DEFAULT: False

samesite

TYPE: Literal['lax', 'strict', 'none'] | None DEFAULT: 'lax'

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
def delete_cookie(
    self,
    key: str,
    path: str = "/",
    domain: str | None = None,
    secure: bool = False,
    httponly: bool = False,
    samesite: typing.Literal["lax", "strict", "none"] | None = "lax",
) -> None:
    self.set_cookie(
        key,
        max_age=0,
        expires=0,
        path=path,
        domain=domain,
        secure=secure,
        httponly=httponly,
        samesite=samesite,
    )

fastapi.responses.StreamingResponse

StreamingResponse(<span data-autorefs-optional="fastapi.responses.StreamingResponse(content)">content</span>, <span data-autorefs-optional="fastapi.responses.StreamingResponse(status_code)">status_code</span>=200, <span data-autorefs-optional="fastapi.responses.StreamingResponse(headers)">headers</span>=None, <span data-autorefs-optional="fastapi.responses.StreamingResponse(media_type)">media_type</span>=None, <span data-autorefs-optional="fastapi.responses.StreamingResponse(background)">background</span>=None)

Bases: Response

PARAMETER DESCRIPTION
content

TYPE: ContentStream

status_code

TYPE: int DEFAULT: 200

headers

TYPE: Mapping[str, str] | None DEFAULT: None

media_type

TYPE: str | None DEFAULT: None

background

TYPE: BackgroundTask | None DEFAULT: None

Source code in starlette/responses.py
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
def __init__(
    self,
    content: ContentStream,
    status_code: int = 200,
    headers: typing.Mapping[str, str] | None = None,
    media_type: str | None = None,
    background: BackgroundTask | None = None,
) -> None:
    if isinstance(content, typing.AsyncIterable):
        self.body_iterator = content
    else:
        self.body_iterator = iterate_in_threadpool(content)
    self.status_code = status_code
    self.media_type = self.media_type if media_type is None else media_type
    self.background = background
    self.init_headers(headers)

body_iterator instance-attribute

body_iterator

charset class-attribute instance-attribute

charset = 'utf-8'

status_code instance-attribute

status_code = status_code

media_type instance-attribute

media_type = (
    media_type if media_type is None else media_type
)

body instance-attribute

body = render(content)

background instance-attribute

background = background

headers property

headers

render

render(<span data-autorefs-optional="fastapi.responses.StreamingResponse.render(content)">content</span>)
PARAMETER DESCRIPTION
content

TYPE: Any

Source code in starlette/responses.py
44
45
46
47
48
49
def render(self, content: typing.Any) -> bytes:
    if content is None:
        return b""
    if isinstance(content, bytes):
        return content
    return content.encode(self.charset)  # type: ignore

init_headers

init_headers(<span data-autorefs-optional="fastapi.responses.StreamingResponse.init_headers(headers)">headers</span>=None)
PARAMETER DESCRIPTION
headers

TYPE: Mapping[str, str] | None DEFAULT: None

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
def init_headers(self, headers: typing.Mapping[str, str] | None = None) -> None:
    if headers is None:
        raw_headers: list[tuple[bytes, bytes]] = []
        populate_content_length = True
        populate_content_type = True
    else:
        raw_headers = [
            (k.lower().encode("latin-1"), v.encode("latin-1"))
            for k, v in headers.items()
        ]
        keys = [h[0] for h in raw_headers]
        populate_content_length = b"content-length" not in keys
        populate_content_type = b"content-type" not in keys

    body = getattr(self, "body", None)
    if (
        body is not None
        and populate_content_length
        and not (self.status_code < 200 or self.status_code in (204, 304))
    ):
        content_length = str(len(body))
        raw_headers.append((b"content-length", content_length.encode("latin-1")))

    content_type = self.media_type
    if content_type is not None and populate_content_type:
        if (
            content_type.startswith("text/")
            and "charset=" not in content_type.lower()
        ):
            content_type += "; charset=" + self.charset
        raw_headers.append((b"content-type", content_type.encode("latin-1")))

    self.raw_headers = raw_headers
set_cookie(<span data-autorefs-optional="fastapi.responses.StreamingResponse.set_cookie(key)">key</span>, <span data-autorefs-optional="fastapi.responses.StreamingResponse.set_cookie(value)">value</span>='', <span data-autorefs-optional="fastapi.responses.StreamingResponse.set_cookie(max_age)">max_age</span>=None, <span data-autorefs-optional="fastapi.responses.StreamingResponse.set_cookie(expires)">expires</span>=None, <span data-autorefs-optional="fastapi.responses.StreamingResponse.set_cookie(path)">path</span>='/', <span data-autorefs-optional="fastapi.responses.StreamingResponse.set_cookie(domain)">domain</span>=None, <span data-autorefs-optional="fastapi.responses.StreamingResponse.set_cookie(secure)">secure</span>=False, <span data-autorefs-optional="fastapi.responses.StreamingResponse.set_cookie(httponly)">httponly</span>=False, <span data-autorefs-optional="fastapi.responses.StreamingResponse.set_cookie(samesite)">samesite</span>='lax')
PARAMETER DESCRIPTION
key

TYPE: str

value

TYPE: str DEFAULT: ''

max_age

TYPE: int | None DEFAULT: None

expires

TYPE: datetime | str | int | None DEFAULT: None

path

TYPE: str DEFAULT: '/'

domain

TYPE: str | None DEFAULT: None

secure

TYPE: bool DEFAULT: False

httponly

TYPE: bool DEFAULT: False

samesite

TYPE: Literal['lax', 'strict', 'none'] | None DEFAULT: 'lax'

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
def set_cookie(
    self,
    key: str,
    value: str = "",
    max_age: int | None = None,
    expires: datetime | str | int | None = None,
    path: str = "/",
    domain: str | None = None,
    secure: bool = False,
    httponly: bool = False,
    samesite: typing.Literal["lax", "strict", "none"] | None = "lax",
) -> None:
    cookie: http.cookies.BaseCookie[str] = http.cookies.SimpleCookie()
    cookie[key] = value
    if max_age is not None:
        cookie[key]["max-age"] = max_age
    if expires is not None:
        if isinstance(expires, datetime):
            cookie[key]["expires"] = format_datetime(expires, usegmt=True)
        else:
            cookie[key]["expires"] = expires
    if path is not None:
        cookie[key]["path"] = path
    if domain is not None:
        cookie[key]["domain"] = domain
    if secure:
        cookie[key]["secure"] = True
    if httponly:
        cookie[key]["httponly"] = True
    if samesite is not None:
        assert samesite.lower() in [
            "strict",
            "lax",
            "none",
        ], "samesite must be either 'strict', 'lax' or 'none'"
        cookie[key]["samesite"] = samesite
    cookie_val = cookie.output(header="").strip()
    self.raw_headers.append((b"set-cookie", cookie_val.encode("latin-1")))
delete_cookie(<span data-autorefs-optional="fastapi.responses.StreamingResponse.delete_cookie(key)">key</span>, <span data-autorefs-optional="fastapi.responses.StreamingResponse.delete_cookie(path)">path</span>='/', <span data-autorefs-optional="fastapi.responses.StreamingResponse.delete_cookie(domain)">domain</span>=None, <span data-autorefs-optional="fastapi.responses.StreamingResponse.delete_cookie(secure)">secure</span>=False, <span data-autorefs-optional="fastapi.responses.StreamingResponse.delete_cookie(httponly)">httponly</span>=False, <span data-autorefs-optional="fastapi.responses.StreamingResponse.delete_cookie(samesite)">samesite</span>='lax')
PARAMETER DESCRIPTION
key

TYPE: str

path

TYPE: str DEFAULT: '/'

domain

TYPE: str | None DEFAULT: None

secure

TYPE: bool DEFAULT: False

httponly

TYPE: bool DEFAULT: False

samesite

TYPE: Literal['lax', 'strict', 'none'] | None DEFAULT: 'lax'

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
def delete_cookie(
    self,
    key: str,
    path: str = "/",
    domain: str | None = None,
    secure: bool = False,
    httponly: bool = False,
    samesite: typing.Literal["lax", "strict", "none"] | None = "lax",
) -> None:
    self.set_cookie(
        key,
        max_age=0,
        expires=0,
        path=path,
        domain=domain,
        secure=secure,
        httponly=httponly,
        samesite=samesite,
    )