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:

  1. from fastapi.responses import (
  2. FileResponse,
  3. HTMLResponse,
  4. JSONResponse,
  5. ORJSONResponse,
  6. PlainTextResponse,
  7. RedirectResponse,
  8. Response,
  9. StreamingResponse,
  10. UJSONResponse,
  11. )

FastAPI Responses

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

fastapi.responses.UJSONResponse

  1. UJSONResponse(
  2. content,
  3. status_code=200,
  4. headers=None,
  5. media_type=None,
  6. background=None,
  7. )

Bases: [JSONResponse](#fastapi.responses.JSONResponse "starlette.responses.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.

PARAMETERDESCRIPTION
content

TYPE: Any

status_code

TYPE: int DEFAULT: 200

headers

TYPE: Optional[Dict[str, str]] DEFAULT: None

media_type

TYPE: Optional[str] DEFAULT: None

background

TYPE: Optional[BackgroundTask] DEFAULT: None

Source code in starlette/responses.py

  1. 188
  2. 189
  3. 190
  4. 191
  5. 192
  6. 193
  7. 194
  8. 195
  9. 196
  1. def init(
  2. self,
  3. content: typing.Any,
  4. statuscode: int = 200,
  5. headers: typing.Optional[typing.Dict[str, str]] = None,
  6. mediatype: typing.Optional[str] = None,
  7. background: typing.Optional[BackgroundTask] = None,
  8. ) -> None:
  9. super().__init(content, status_code, headers, media_type, background)

charset class-attribute instance-attribute

  1. charset = 'utf-8'

status_code instance-attribute

  1. status_code = status_code

media_type class-attribute instance-attribute

  1. media_type = 'application/json'

body instance-attribute

  1. body = render(content)

background instance-attribute

  1. background = background

headers property

  1. headers

render

  1. render(content)
PARAMETERDESCRIPTION
content

TYPE: Any

Source code in fastapi/responses.py

  1. 31
  2. 32
  3. 33
  1. def render(self, content: Any) -> bytes:
  2. assert ujson is not None, ujson must be installed to use UJSONResponse
  3. return ujson.dumps(content, ensure_ascii=False).encode(utf-8)

init_headers

  1. init_headers(headers=None)
PARAMETERDESCRIPTION
headers

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

Source code in starlette/responses.py

  1. 65
  2. 66
  3. 67
  4. 68
  5. 69
  6. 70
  7. 71
  8. 72
  9. 73
  10. 74
  11. 75
  12. 76
  13. 77
  14. 78
  15. 79
  16. 80
  17. 81
  18. 82
  19. 83
  20. 84
  21. 85
  22. 86
  23. 87
  24. 88
  25. 89
  26. 90
  27. 91
  28. 92
  29. 93
  30. 94
  31. 95
  32. 96
  1. def init_headers(
  2. self, headers: typing.Optional[typing.Mapping[str, str]] = None
  3. ) -> None:
  4. if headers is None:
  5. raw_headers: typing.List[typing.Tuple[bytes, bytes]] = []
  6. populate_content_length = True
  7. populate_content_type = True
  8. else:
  9. raw_headers = [
  10. (k.lower().encode(latin-1), v.encode(latin-1))
  11. for k, v in headers.items()
  12. ]
  13. keys = [h[0] for h in raw_headers]
  14. populate_content_length = bcontent-length not in keys
  15. populate_content_type = bcontent-type not in keys
  16. body = getattr(self, body, None)
  17. if (
  18. body is not None
  19. and populate_content_length
  20. and not (self.status_code < 200 or self.status_code in (204, 304))
  21. ):
  22. content_length = str(len(body))
  23. raw_headers.append((bcontent-length, content_length.encode(latin-1)))
  24. content_type = self.media_type
  25. if content_type is not None and populate_content_type:
  26. if content_type.startswith(text/“):
  27. content_type += “; charset=” + self.charset
  28. raw_headers.append((bcontent-type, content_type.encode(latin-1)))
  29. self.raw_headers = raw_headers
  1. set_cookie(
  2. key,
  3. value="",
  4. max_age=None,
  5. expires=None,
  6. path="/",
  7. domain=None,
  8. secure=False,
  9. httponly=False,
  10. samesite="lax",
  11. )
PARAMETERDESCRIPTION
key

TYPE: str

value

TYPE: str DEFAULT: ‘’

max_age

TYPE: Optional[int] DEFAULT: None

expires

TYPE: Optional[Union[datetime, str, int]] DEFAULT: None

path

TYPE: str DEFAULT: ‘/‘

domain

TYPE: Optional[str] DEFAULT: None

secure

TYPE: bool DEFAULT: False

httponly

TYPE: bool DEFAULT: False

samesite

TYPE: Optional[Literal[‘lax’, ‘strict’, ‘none’]] DEFAULT: ‘lax’

Source code in starlette/responses.py

  1. 104
  2. 105
  3. 106
  4. 107
  5. 108
  6. 109
  7. 110
  8. 111
  9. 112
  10. 113
  11. 114
  12. 115
  13. 116
  14. 117
  15. 118
  16. 119
  17. 120
  18. 121
  19. 122
  20. 123
  21. 124
  22. 125
  23. 126
  24. 127
  25. 128
  26. 129
  27. 130
  28. 131
  29. 132
  30. 133
  31. 134
  32. 135
  33. 136
  34. 137
  35. 138
  36. 139
  37. 140
  38. 141
  1. def set_cookie(
  2. self,
  3. key: str,
  4. value: str = “”,
  5. max_age: typing.Optional[int] = None,
  6. expires: typing.Optional[typing.Union[datetime, str, int]] = None,
  7. path: str = “/“,
  8. domain: typing.Optional[str] = None,
  9. secure: bool = False,
  10. httponly: bool = False,
  11. samesite: typing.Optional[Literal[lax, strict, none]] = lax,
  12. ) -> None:
  13. cookie: http.cookies.BaseCookie[str]” = http.cookies.SimpleCookie()
  14. cookie[key] = value
  15. if max_age is not None:
  16. cookie[key][max-age] = max_age
  17. if expires is not None:
  18. if isinstance(expires, datetime):
  19. cookie[key][expires] = format_datetime(expires, usegmt=True)
  20. else:
  21. cookie[key][expires] = expires
  22. if path is not None:
  23. cookie[key][path] = path
  24. if domain is not None:
  25. cookie[key][domain] = domain
  26. if secure:
  27. cookie[key][secure] = True
  28. if httponly:
  29. cookie[key][httponly] = True
  30. if samesite is not None:
  31. assert samesite.lower() in [
  32. strict,
  33. lax,
  34. none,
  35. ], samesite must be either strict’, lax or none’”
  36. cookie[key][samesite] = samesite
  37. cookie_val = cookie.output(header=“”).strip()
  38. self.raw_headers.append((bset-cookie, cookie_val.encode(latin-1)))
  1. delete_cookie(
  2. key,
  3. path="/",
  4. domain=None,
  5. secure=False,
  6. httponly=False,
  7. samesite="lax",
  8. )
PARAMETERDESCRIPTION
key

TYPE: str

path

TYPE: str DEFAULT: ‘/‘

domain

TYPE: Optional[str] DEFAULT: None

secure

TYPE: bool DEFAULT: False

httponly

TYPE: bool DEFAULT: False

samesite

TYPE: Optional[Literal[‘lax’, ‘strict’, ‘none’]] DEFAULT: ‘lax’

Source code in starlette/responses.py

  1. 143
  2. 144
  3. 145
  4. 146
  5. 147
  6. 148
  7. 149
  8. 150
  9. 151
  10. 152
  11. 153
  12. 154
  13. 155
  14. 156
  15. 157
  16. 158
  17. 159
  18. 160
  19. 161
  1. def delete_cookie(
  2. self,
  3. key: str,
  4. path: str = “/“,
  5. domain: typing.Optional[str] = None,
  6. secure: bool = False,
  7. httponly: bool = False,
  8. samesite: typing.Optional[Literal[lax, strict, none]] = lax,
  9. ) -> None:
  10. self.set_cookie(
  11. key,
  12. max_age=0,
  13. expires=0,
  14. path=path,
  15. domain=domain,
  16. secure=secure,
  17. httponly=httponly,
  18. samesite=samesite,
  19. )

fastapi.responses.ORJSONResponse

  1. ORJSONResponse(
  2. content,
  3. status_code=200,
  4. headers=None,
  5. media_type=None,
  6. background=None,
  7. )

Bases: [JSONResponse](#fastapi.responses.JSONResponse "starlette.responses.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.

PARAMETERDESCRIPTION
content

TYPE: Any

status_code

TYPE: int DEFAULT: 200

headers

TYPE: Optional[Dict[str, str]] DEFAULT: None

media_type

TYPE: Optional[str] DEFAULT: None

background

TYPE: Optional[BackgroundTask] DEFAULT: None

Source code in starlette/responses.py

  1. 188
  2. 189
  3. 190
  4. 191
  5. 192
  6. 193
  7. 194
  8. 195
  9. 196
  1. def init(
  2. self,
  3. content: typing.Any,
  4. statuscode: int = 200,
  5. headers: typing.Optional[typing.Dict[str, str]] = None,
  6. mediatype: typing.Optional[str] = None,
  7. background: typing.Optional[BackgroundTask] = None,
  8. ) -> None:
  9. super().__init(content, status_code, headers, media_type, background)

charset class-attribute instance-attribute

  1. charset = 'utf-8'

status_code instance-attribute

  1. status_code = status_code

media_type class-attribute instance-attribute

  1. media_type = 'application/json'

body instance-attribute

  1. body = render(content)

background instance-attribute

  1. background = background

headers property

  1. headers

render

  1. render(content)
PARAMETERDESCRIPTION
content

TYPE: Any

Source code in fastapi/responses.py

  1. 44
  2. 45
  3. 46
  4. 47
  5. 48
  1. def render(self, content: Any) -> bytes:
  2. assert orjson is not None, orjson must be installed to use ORJSONResponse
  3. return orjson.dumps(
  4. content, option=orjson.OPT_NON_STR_KEYS | orjson.OPT_SERIALIZE_NUMPY
  5. )

init_headers

  1. init_headers(headers=None)
PARAMETERDESCRIPTION
headers

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

Source code in starlette/responses.py

  1. 65
  2. 66
  3. 67
  4. 68
  5. 69
  6. 70
  7. 71
  8. 72
  9. 73
  10. 74
  11. 75
  12. 76
  13. 77
  14. 78
  15. 79
  16. 80
  17. 81
  18. 82
  19. 83
  20. 84
  21. 85
  22. 86
  23. 87
  24. 88
  25. 89
  26. 90
  27. 91
  28. 92
  29. 93
  30. 94
  31. 95
  32. 96
  1. def init_headers(
  2. self, headers: typing.Optional[typing.Mapping[str, str]] = None
  3. ) -> None:
  4. if headers is None:
  5. raw_headers: typing.List[typing.Tuple[bytes, bytes]] = []
  6. populate_content_length = True
  7. populate_content_type = True
  8. else:
  9. raw_headers = [
  10. (k.lower().encode(latin-1), v.encode(latin-1))
  11. for k, v in headers.items()
  12. ]
  13. keys = [h[0] for h in raw_headers]
  14. populate_content_length = bcontent-length not in keys
  15. populate_content_type = bcontent-type not in keys
  16. body = getattr(self, body, None)
  17. if (
  18. body is not None
  19. and populate_content_length
  20. and not (self.status_code < 200 or self.status_code in (204, 304))
  21. ):
  22. content_length = str(len(body))
  23. raw_headers.append((bcontent-length, content_length.encode(latin-1)))
  24. content_type = self.media_type
  25. if content_type is not None and populate_content_type:
  26. if content_type.startswith(text/“):
  27. content_type += “; charset=” + self.charset
  28. raw_headers.append((bcontent-type, content_type.encode(latin-1)))
  29. self.raw_headers = raw_headers
  1. set_cookie(
  2. key,
  3. value="",
  4. max_age=None,
  5. expires=None,
  6. path="/",
  7. domain=None,
  8. secure=False,
  9. httponly=False,
  10. samesite="lax",
  11. )
PARAMETERDESCRIPTION
key

TYPE: str

value

TYPE: str DEFAULT: ‘’

max_age

TYPE: Optional[int] DEFAULT: None

expires

TYPE: Optional[Union[datetime, str, int]] DEFAULT: None

path

TYPE: str DEFAULT: ‘/‘

domain

TYPE: Optional[str] DEFAULT: None

secure

TYPE: bool DEFAULT: False

httponly

TYPE: bool DEFAULT: False

samesite

TYPE: Optional[Literal[‘lax’, ‘strict’, ‘none’]] DEFAULT: ‘lax’

Source code in starlette/responses.py

  1. 104
  2. 105
  3. 106
  4. 107
  5. 108
  6. 109
  7. 110
  8. 111
  9. 112
  10. 113
  11. 114
  12. 115
  13. 116
  14. 117
  15. 118
  16. 119
  17. 120
  18. 121
  19. 122
  20. 123
  21. 124
  22. 125
  23. 126
  24. 127
  25. 128
  26. 129
  27. 130
  28. 131
  29. 132
  30. 133
  31. 134
  32. 135
  33. 136
  34. 137
  35. 138
  36. 139
  37. 140
  38. 141
  1. def set_cookie(
  2. self,
  3. key: str,
  4. value: str = “”,
  5. max_age: typing.Optional[int] = None,
  6. expires: typing.Optional[typing.Union[datetime, str, int]] = None,
  7. path: str = “/“,
  8. domain: typing.Optional[str] = None,
  9. secure: bool = False,
  10. httponly: bool = False,
  11. samesite: typing.Optional[Literal[lax, strict, none]] = lax,
  12. ) -> None:
  13. cookie: http.cookies.BaseCookie[str]” = http.cookies.SimpleCookie()
  14. cookie[key] = value
  15. if max_age is not None:
  16. cookie[key][max-age] = max_age
  17. if expires is not None:
  18. if isinstance(expires, datetime):
  19. cookie[key][expires] = format_datetime(expires, usegmt=True)
  20. else:
  21. cookie[key][expires] = expires
  22. if path is not None:
  23. cookie[key][path] = path
  24. if domain is not None:
  25. cookie[key][domain] = domain
  26. if secure:
  27. cookie[key][secure] = True
  28. if httponly:
  29. cookie[key][httponly] = True
  30. if samesite is not None:
  31. assert samesite.lower() in [
  32. strict,
  33. lax,
  34. none,
  35. ], samesite must be either strict’, lax or none’”
  36. cookie[key][samesite] = samesite
  37. cookie_val = cookie.output(header=“”).strip()
  38. self.raw_headers.append((bset-cookie, cookie_val.encode(latin-1)))
  1. delete_cookie(
  2. key,
  3. path="/",
  4. domain=None,
  5. secure=False,
  6. httponly=False,
  7. samesite="lax",
  8. )
PARAMETERDESCRIPTION
key

TYPE: str

path

TYPE: str DEFAULT: ‘/‘

domain

TYPE: Optional[str] DEFAULT: None

secure

TYPE: bool DEFAULT: False

httponly

TYPE: bool DEFAULT: False

samesite

TYPE: Optional[Literal[‘lax’, ‘strict’, ‘none’]] DEFAULT: ‘lax’

Source code in starlette/responses.py

  1. 143
  2. 144
  3. 145
  4. 146
  5. 147
  6. 148
  7. 149
  8. 150
  9. 151
  10. 152
  11. 153
  12. 154
  13. 155
  14. 156
  15. 157
  16. 158
  17. 159
  18. 160
  19. 161
  1. def delete_cookie(
  2. self,
  3. key: str,
  4. path: str = “/“,
  5. domain: typing.Optional[str] = None,
  6. secure: bool = False,
  7. httponly: bool = False,
  8. samesite: typing.Optional[Literal[lax, strict, none]] = lax,
  9. ) -> None:
  10. self.set_cookie(
  11. key,
  12. max_age=0,
  13. expires=0,
  14. path=path,
  15. domain=domain,
  16. secure=secure,
  17. httponly=httponly,
  18. samesite=samesite,
  19. )

Starlette Responses

fastapi.responses.FileResponse

  1. FileResponse(
  2. path,
  3. status_code=200,
  4. headers=None,
  5. media_type=None,
  6. background=None,
  7. filename=None,
  8. stat_result=None,
  9. method=None,
  10. content_disposition_type="attachment",
  11. )

Bases: [Response](#fastapi.responses.Response "starlette.responses.Response")

PARAMETERDESCRIPTION
path

TYPE: Union[str, PathLike[str]]

status_code

TYPE: int DEFAULT: 200

headers

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

media_type

TYPE: Optional[str] DEFAULT: None

background

TYPE: Optional[BackgroundTask] DEFAULT: None

filename

TYPE: Optional[str] DEFAULT: None

stat_result

TYPE: Optional[stat_result] DEFAULT: None

method

TYPE: Optional[str] DEFAULT: None

content_disposition_type

TYPE: str DEFAULT: ‘attachment’

Source code in starlette/responses.py

  1. 286
  2. 287
  3. 288
  4. 289
  5. 290
  6. 291
  7. 292
  8. 293
  9. 294
  10. 295
  11. 296
  12. 297
  13. 298
  14. 299
  15. 300
  16. 301
  17. 302
  18. 303
  19. 304
  20. 305
  21. 306
  22. 307
  23. 308
  24. 309
  25. 310
  26. 311
  27. 312
  28. 313
  29. 314
  30. 315
  31. 316
  32. 317
  33. 318
  34. 319
  35. 320
  1. def init(
  2. self,
  3. path: typing.Union[str, os.PathLike[str]”],
  4. status_code: int = 200,
  5. headers: typing.Optional[typing.Mapping[str, str]] = None,
  6. media_type: typing.Optional[str] = None,
  7. background: typing.Optional[BackgroundTask] = None,
  8. filename: typing.Optional[str] = None,
  9. stat_result: typing.Optional[os.stat_result] = None,
  10. method: typing.Optional[str] = None,
  11. content_disposition_type: str = attachment,
  12. ) -> None:
  13. self.path = path
  14. self.status_code = status_code
  15. self.filename = filename
  16. self.send_header_only = method is not None and method.upper() == HEAD
  17. if media_type is None:
  18. media_type = guess_type(filename or path)[0] or text/plain
  19. self.media_type = media_type
  20. self.background = background
  21. self.init_headers(headers)
  22. if self.filename is not None:
  23. content_disposition_filename = quote(self.filename)
  24. if content_disposition_filename != self.filename:
  25. content_disposition = {}; filename*=utf-8’’{}.format(
  26. content_disposition_type, content_disposition_filename
  27. )
  28. else:
  29. content_disposition = {}; filename=”{}“‘.format(
  30. content_disposition_type, self.filename
  31. )
  32. self.headers.setdefault(content-disposition, content_disposition)
  33. self.stat_result = stat_result
  34. if stat_result is not None:
  35. self.set_stat_headers(stat_result)

chunk_size class-attribute instance-attribute

  1. chunk_size = 64 * 1024

charset class-attribute instance-attribute

  1. charset = 'utf-8'

status_code instance-attribute

  1. status_code = status_code

media_type instance-attribute

  1. media_type = media_type

body instance-attribute

  1. body = render(content)

background instance-attribute

  1. background = background

headers property

  1. headers

render

  1. render(content)
PARAMETERDESCRIPTION
content

TYPE: Any

Source code in starlette/responses.py

  1. 58
  2. 59
  3. 60
  4. 61
  5. 62
  6. 63
  1. def render(self, content: typing.Any) -> bytes:
  2. if content is None:
  3. return b“”
  4. if isinstance(content, bytes):
  5. return content
  6. return content.encode(self.charset)

init_headers

  1. init_headers(headers=None)
PARAMETERDESCRIPTION
headers

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

Source code in starlette/responses.py

  1. 65
  2. 66
  3. 67
  4. 68
  5. 69
  6. 70
  7. 71
  8. 72
  9. 73
  10. 74
  11. 75
  12. 76
  13. 77
  14. 78
  15. 79
  16. 80
  17. 81
  18. 82
  19. 83
  20. 84
  21. 85
  22. 86
  23. 87
  24. 88
  25. 89
  26. 90
  27. 91
  28. 92
  29. 93
  30. 94
  31. 95
  32. 96
  1. def init_headers(
  2. self, headers: typing.Optional[typing.Mapping[str, str]] = None
  3. ) -> None:
  4. if headers is None:
  5. raw_headers: typing.List[typing.Tuple[bytes, bytes]] = []
  6. populate_content_length = True
  7. populate_content_type = True
  8. else:
  9. raw_headers = [
  10. (k.lower().encode(latin-1), v.encode(latin-1))
  11. for k, v in headers.items()
  12. ]
  13. keys = [h[0] for h in raw_headers]
  14. populate_content_length = bcontent-length not in keys
  15. populate_content_type = bcontent-type not in keys
  16. body = getattr(self, body, None)
  17. if (
  18. body is not None
  19. and populate_content_length
  20. and not (self.status_code < 200 or self.status_code in (204, 304))
  21. ):
  22. content_length = str(len(body))
  23. raw_headers.append((bcontent-length, content_length.encode(latin-1)))
  24. content_type = self.media_type
  25. if content_type is not None and populate_content_type:
  26. if content_type.startswith(text/“):
  27. content_type += “; charset=” + self.charset
  28. raw_headers.append((bcontent-type, content_type.encode(latin-1)))
  29. self.raw_headers = raw_headers
  1. set_cookie(
  2. key,
  3. value="",
  4. max_age=None,
  5. expires=None,
  6. path="/",
  7. domain=None,
  8. secure=False,
  9. httponly=False,
  10. samesite="lax",
  11. )
PARAMETERDESCRIPTION
key

TYPE: str

value

TYPE: str DEFAULT: ‘’

max_age

TYPE: Optional[int] DEFAULT: None

expires

TYPE: Optional[Union[datetime, str, int]] DEFAULT: None

path

TYPE: str DEFAULT: ‘/‘

domain

TYPE: Optional[str] DEFAULT: None

secure

TYPE: bool DEFAULT: False

httponly

TYPE: bool DEFAULT: False

samesite

TYPE: Optional[Literal[‘lax’, ‘strict’, ‘none’]] DEFAULT: ‘lax’

Source code in starlette/responses.py

  1. 104
  2. 105
  3. 106
  4. 107
  5. 108
  6. 109
  7. 110
  8. 111
  9. 112
  10. 113
  11. 114
  12. 115
  13. 116
  14. 117
  15. 118
  16. 119
  17. 120
  18. 121
  19. 122
  20. 123
  21. 124
  22. 125
  23. 126
  24. 127
  25. 128
  26. 129
  27. 130
  28. 131
  29. 132
  30. 133
  31. 134
  32. 135
  33. 136
  34. 137
  35. 138
  36. 139
  37. 140
  38. 141
  1. def set_cookie(
  2. self,
  3. key: str,
  4. value: str = “”,
  5. max_age: typing.Optional[int] = None,
  6. expires: typing.Optional[typing.Union[datetime, str, int]] = None,
  7. path: str = “/“,
  8. domain: typing.Optional[str] = None,
  9. secure: bool = False,
  10. httponly: bool = False,
  11. samesite: typing.Optional[Literal[lax, strict, none]] = lax,
  12. ) -> None:
  13. cookie: http.cookies.BaseCookie[str]” = http.cookies.SimpleCookie()
  14. cookie[key] = value
  15. if max_age is not None:
  16. cookie[key][max-age] = max_age
  17. if expires is not None:
  18. if isinstance(expires, datetime):
  19. cookie[key][expires] = format_datetime(expires, usegmt=True)
  20. else:
  21. cookie[key][expires] = expires
  22. if path is not None:
  23. cookie[key][path] = path
  24. if domain is not None:
  25. cookie[key][domain] = domain
  26. if secure:
  27. cookie[key][secure] = True
  28. if httponly:
  29. cookie[key][httponly] = True
  30. if samesite is not None:
  31. assert samesite.lower() in [
  32. strict,
  33. lax,
  34. none,
  35. ], samesite must be either strict’, lax or none’”
  36. cookie[key][samesite] = samesite
  37. cookie_val = cookie.output(header=“”).strip()
  38. self.raw_headers.append((bset-cookie, cookie_val.encode(latin-1)))
  1. delete_cookie(
  2. key,
  3. path="/",
  4. domain=None,
  5. secure=False,
  6. httponly=False,
  7. samesite="lax",
  8. )
PARAMETERDESCRIPTION
key

TYPE: str

path

TYPE: str DEFAULT: ‘/‘

domain

TYPE: Optional[str] DEFAULT: None

secure

TYPE: bool DEFAULT: False

httponly

TYPE: bool DEFAULT: False

samesite

TYPE: Optional[Literal[‘lax’, ‘strict’, ‘none’]] DEFAULT: ‘lax’

Source code in starlette/responses.py

  1. 143
  2. 144
  3. 145
  4. 146
  5. 147
  6. 148
  7. 149
  8. 150
  9. 151
  10. 152
  11. 153
  12. 154
  13. 155
  14. 156
  15. 157
  16. 158
  17. 159
  18. 160
  19. 161
  1. def delete_cookie(
  2. self,
  3. key: str,
  4. path: str = “/“,
  5. domain: typing.Optional[str] = None,
  6. secure: bool = False,
  7. httponly: bool = False,
  8. samesite: typing.Optional[Literal[lax, strict, none]] = lax,
  9. ) -> None:
  10. self.set_cookie(
  11. key,
  12. max_age=0,
  13. expires=0,
  14. path=path,
  15. domain=domain,
  16. secure=secure,
  17. httponly=httponly,
  18. samesite=samesite,
  19. )

fastapi.responses.HTMLResponse

  1. HTMLResponse(
  2. content=None,
  3. status_code=200,
  4. headers=None,
  5. media_type=None,
  6. background=None,
  7. )

Bases: [Response](#fastapi.responses.Response "starlette.responses.Response")

PARAMETERDESCRIPTION
content

TYPE: Any DEFAULT: None

status_code

TYPE: int DEFAULT: 200

headers

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

media_type

TYPE: Optional[str] DEFAULT: None

background

TYPE: Optional[BackgroundTask] DEFAULT: None

Source code in starlette/responses.py

  1. 43
  2. 44
  3. 45
  4. 46
  5. 47
  6. 48
  7. 49
  8. 50
  9. 51
  10. 52
  11. 53
  12. 54
  13. 55
  14. 56
  1. def init(
  2. self,
  3. content: typing.Any = None,
  4. status_code: int = 200,
  5. headers: typing.Optional[typing.Mapping[str, str]] = None,
  6. media_type: typing.Optional[str] = None,
  7. background: typing.Optional[BackgroundTask] = None,
  8. ) -> None:
  9. self.status_code = status_code
  10. if media_type is not None:
  11. self.media_type = media_type
  12. self.background = background
  13. self.body = self.render(content)
  14. self.init_headers(headers)

charset class-attribute instance-attribute

  1. charset = 'utf-8'

status_code instance-attribute

  1. status_code = status_code

media_type class-attribute instance-attribute

  1. media_type = 'text/html'

body instance-attribute

  1. body = render(content)

background instance-attribute

  1. background = background

headers property

  1. headers

render

  1. render(content)
PARAMETERDESCRIPTION
content

TYPE: Any

Source code in starlette/responses.py

  1. 58
  2. 59
  3. 60
  4. 61
  5. 62
  6. 63
  1. def render(self, content: typing.Any) -> bytes:
  2. if content is None:
  3. return b“”
  4. if isinstance(content, bytes):
  5. return content
  6. return content.encode(self.charset)

init_headers

  1. init_headers(headers=None)
PARAMETERDESCRIPTION
headers

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

Source code in starlette/responses.py

  1. 65
  2. 66
  3. 67
  4. 68
  5. 69
  6. 70
  7. 71
  8. 72
  9. 73
  10. 74
  11. 75
  12. 76
  13. 77
  14. 78
  15. 79
  16. 80
  17. 81
  18. 82
  19. 83
  20. 84
  21. 85
  22. 86
  23. 87
  24. 88
  25. 89
  26. 90
  27. 91
  28. 92
  29. 93
  30. 94
  31. 95
  32. 96
  1. def init_headers(
  2. self, headers: typing.Optional[typing.Mapping[str, str]] = None
  3. ) -> None:
  4. if headers is None:
  5. raw_headers: typing.List[typing.Tuple[bytes, bytes]] = []
  6. populate_content_length = True
  7. populate_content_type = True
  8. else:
  9. raw_headers = [
  10. (k.lower().encode(latin-1), v.encode(latin-1))
  11. for k, v in headers.items()
  12. ]
  13. keys = [h[0] for h in raw_headers]
  14. populate_content_length = bcontent-length not in keys
  15. populate_content_type = bcontent-type not in keys
  16. body = getattr(self, body, None)
  17. if (
  18. body is not None
  19. and populate_content_length
  20. and not (self.status_code < 200 or self.status_code in (204, 304))
  21. ):
  22. content_length = str(len(body))
  23. raw_headers.append((bcontent-length, content_length.encode(latin-1)))
  24. content_type = self.media_type
  25. if content_type is not None and populate_content_type:
  26. if content_type.startswith(text/“):
  27. content_type += “; charset=” + self.charset
  28. raw_headers.append((bcontent-type, content_type.encode(latin-1)))
  29. self.raw_headers = raw_headers
  1. set_cookie(
  2. key,
  3. value="",
  4. max_age=None,
  5. expires=None,
  6. path="/",
  7. domain=None,
  8. secure=False,
  9. httponly=False,
  10. samesite="lax",
  11. )
PARAMETERDESCRIPTION
key

TYPE: str

value

TYPE: str DEFAULT: ‘’

max_age

TYPE: Optional[int] DEFAULT: None

expires

TYPE: Optional[Union[datetime, str, int]] DEFAULT: None

path

TYPE: str DEFAULT: ‘/‘

domain

TYPE: Optional[str] DEFAULT: None

secure

TYPE: bool DEFAULT: False

httponly

TYPE: bool DEFAULT: False

samesite

TYPE: Optional[Literal[‘lax’, ‘strict’, ‘none’]] DEFAULT: ‘lax’

Source code in starlette/responses.py

  1. 104
  2. 105
  3. 106
  4. 107
  5. 108
  6. 109
  7. 110
  8. 111
  9. 112
  10. 113
  11. 114
  12. 115
  13. 116
  14. 117
  15. 118
  16. 119
  17. 120
  18. 121
  19. 122
  20. 123
  21. 124
  22. 125
  23. 126
  24. 127
  25. 128
  26. 129
  27. 130
  28. 131
  29. 132
  30. 133
  31. 134
  32. 135
  33. 136
  34. 137
  35. 138
  36. 139
  37. 140
  38. 141
  1. def set_cookie(
  2. self,
  3. key: str,
  4. value: str = “”,
  5. max_age: typing.Optional[int] = None,
  6. expires: typing.Optional[typing.Union[datetime, str, int]] = None,
  7. path: str = “/“,
  8. domain: typing.Optional[str] = None,
  9. secure: bool = False,
  10. httponly: bool = False,
  11. samesite: typing.Optional[Literal[lax, strict, none]] = lax,
  12. ) -> None:
  13. cookie: http.cookies.BaseCookie[str]” = http.cookies.SimpleCookie()
  14. cookie[key] = value
  15. if max_age is not None:
  16. cookie[key][max-age] = max_age
  17. if expires is not None:
  18. if isinstance(expires, datetime):
  19. cookie[key][expires] = format_datetime(expires, usegmt=True)
  20. else:
  21. cookie[key][expires] = expires
  22. if path is not None:
  23. cookie[key][path] = path
  24. if domain is not None:
  25. cookie[key][domain] = domain
  26. if secure:
  27. cookie[key][secure] = True
  28. if httponly:
  29. cookie[key][httponly] = True
  30. if samesite is not None:
  31. assert samesite.lower() in [
  32. strict,
  33. lax,
  34. none,
  35. ], samesite must be either strict’, lax or none’”
  36. cookie[key][samesite] = samesite
  37. cookie_val = cookie.output(header=“”).strip()
  38. self.raw_headers.append((bset-cookie, cookie_val.encode(latin-1)))
  1. delete_cookie(
  2. key,
  3. path="/",
  4. domain=None,
  5. secure=False,
  6. httponly=False,
  7. samesite="lax",
  8. )
PARAMETERDESCRIPTION
key

TYPE: str

path

TYPE: str DEFAULT: ‘/‘

domain

TYPE: Optional[str] DEFAULT: None

secure

TYPE: bool DEFAULT: False

httponly

TYPE: bool DEFAULT: False

samesite

TYPE: Optional[Literal[‘lax’, ‘strict’, ‘none’]] DEFAULT: ‘lax’

Source code in starlette/responses.py

  1. 143
  2. 144
  3. 145
  4. 146
  5. 147
  6. 148
  7. 149
  8. 150
  9. 151
  10. 152
  11. 153
  12. 154
  13. 155
  14. 156
  15. 157
  16. 158
  17. 159
  18. 160
  19. 161
  1. def delete_cookie(
  2. self,
  3. key: str,
  4. path: str = “/“,
  5. domain: typing.Optional[str] = None,
  6. secure: bool = False,
  7. httponly: bool = False,
  8. samesite: typing.Optional[Literal[lax, strict, none]] = lax,
  9. ) -> None:
  10. self.set_cookie(
  11. key,
  12. max_age=0,
  13. expires=0,
  14. path=path,
  15. domain=domain,
  16. secure=secure,
  17. httponly=httponly,
  18. samesite=samesite,
  19. )

fastapi.responses.JSONResponse

  1. JSONResponse(
  2. content,
  3. status_code=200,
  4. headers=None,
  5. media_type=None,
  6. background=None,
  7. )

Bases: [Response](#fastapi.responses.Response "starlette.responses.Response")

PARAMETERDESCRIPTION
content

TYPE: Any

status_code

TYPE: int DEFAULT: 200

headers

TYPE: Optional[Dict[str, str]] DEFAULT: None

media_type

TYPE: Optional[str] DEFAULT: None

background

TYPE: Optional[BackgroundTask] DEFAULT: None

Source code in starlette/responses.py

  1. 188
  2. 189
  3. 190
  4. 191
  5. 192
  6. 193
  7. 194
  8. 195
  9. 196
  1. def init(
  2. self,
  3. content: typing.Any,
  4. statuscode: int = 200,
  5. headers: typing.Optional[typing.Dict[str, str]] = None,
  6. mediatype: typing.Optional[str] = None,
  7. background: typing.Optional[BackgroundTask] = None,
  8. ) -> None:
  9. super().__init(content, status_code, headers, media_type, background)

charset class-attribute instance-attribute

  1. charset = 'utf-8'

status_code instance-attribute

  1. status_code = status_code

media_type class-attribute instance-attribute

  1. media_type = 'application/json'

body instance-attribute

  1. body = render(content)

background instance-attribute

  1. background = background

headers property

  1. headers

render

  1. render(content)
PARAMETERDESCRIPTION
content

TYPE: Any

Source code in starlette/responses.py

  1. 198
  2. 199
  3. 200
  4. 201
  5. 202
  6. 203
  7. 204
  8. 205
  1. def render(self, content: typing.Any) -> bytes:
  2. return json.dumps(
  3. content,
  4. ensure_ascii=False,
  5. allow_nan=False,
  6. indent=None,
  7. separators=(“,”, “:”),
  8. ).encode(utf-8)

init_headers

  1. init_headers(headers=None)
PARAMETERDESCRIPTION
headers

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

Source code in starlette/responses.py

  1. 65
  2. 66
  3. 67
  4. 68
  5. 69
  6. 70
  7. 71
  8. 72
  9. 73
  10. 74
  11. 75
  12. 76
  13. 77
  14. 78
  15. 79
  16. 80
  17. 81
  18. 82
  19. 83
  20. 84
  21. 85
  22. 86
  23. 87
  24. 88
  25. 89
  26. 90
  27. 91
  28. 92
  29. 93
  30. 94
  31. 95
  32. 96
  1. def init_headers(
  2. self, headers: typing.Optional[typing.Mapping[str, str]] = None
  3. ) -> None:
  4. if headers is None:
  5. raw_headers: typing.List[typing.Tuple[bytes, bytes]] = []
  6. populate_content_length = True
  7. populate_content_type = True
  8. else:
  9. raw_headers = [
  10. (k.lower().encode(latin-1), v.encode(latin-1))
  11. for k, v in headers.items()
  12. ]
  13. keys = [h[0] for h in raw_headers]
  14. populate_content_length = bcontent-length not in keys
  15. populate_content_type = bcontent-type not in keys
  16. body = getattr(self, body, None)
  17. if (
  18. body is not None
  19. and populate_content_length
  20. and not (self.status_code < 200 or self.status_code in (204, 304))
  21. ):
  22. content_length = str(len(body))
  23. raw_headers.append((bcontent-length, content_length.encode(latin-1)))
  24. content_type = self.media_type
  25. if content_type is not None and populate_content_type:
  26. if content_type.startswith(text/“):
  27. content_type += “; charset=” + self.charset
  28. raw_headers.append((bcontent-type, content_type.encode(latin-1)))
  29. self.raw_headers = raw_headers
  1. set_cookie(
  2. key,
  3. value="",
  4. max_age=None,
  5. expires=None,
  6. path="/",
  7. domain=None,
  8. secure=False,
  9. httponly=False,
  10. samesite="lax",
  11. )
PARAMETERDESCRIPTION
key

TYPE: str

value

TYPE: str DEFAULT: ‘’

max_age

TYPE: Optional[int] DEFAULT: None

expires

TYPE: Optional[Union[datetime, str, int]] DEFAULT: None

path

TYPE: str DEFAULT: ‘/‘

domain

TYPE: Optional[str] DEFAULT: None

secure

TYPE: bool DEFAULT: False

httponly

TYPE: bool DEFAULT: False

samesite

TYPE: Optional[Literal[‘lax’, ‘strict’, ‘none’]] DEFAULT: ‘lax’

Source code in starlette/responses.py

  1. 104
  2. 105
  3. 106
  4. 107
  5. 108
  6. 109
  7. 110
  8. 111
  9. 112
  10. 113
  11. 114
  12. 115
  13. 116
  14. 117
  15. 118
  16. 119
  17. 120
  18. 121
  19. 122
  20. 123
  21. 124
  22. 125
  23. 126
  24. 127
  25. 128
  26. 129
  27. 130
  28. 131
  29. 132
  30. 133
  31. 134
  32. 135
  33. 136
  34. 137
  35. 138
  36. 139
  37. 140
  38. 141
  1. def set_cookie(
  2. self,
  3. key: str,
  4. value: str = “”,
  5. max_age: typing.Optional[int] = None,
  6. expires: typing.Optional[typing.Union[datetime, str, int]] = None,
  7. path: str = “/“,
  8. domain: typing.Optional[str] = None,
  9. secure: bool = False,
  10. httponly: bool = False,
  11. samesite: typing.Optional[Literal[lax, strict, none]] = lax,
  12. ) -> None:
  13. cookie: http.cookies.BaseCookie[str]” = http.cookies.SimpleCookie()
  14. cookie[key] = value
  15. if max_age is not None:
  16. cookie[key][max-age] = max_age
  17. if expires is not None:
  18. if isinstance(expires, datetime):
  19. cookie[key][expires] = format_datetime(expires, usegmt=True)
  20. else:
  21. cookie[key][expires] = expires
  22. if path is not None:
  23. cookie[key][path] = path
  24. if domain is not None:
  25. cookie[key][domain] = domain
  26. if secure:
  27. cookie[key][secure] = True
  28. if httponly:
  29. cookie[key][httponly] = True
  30. if samesite is not None:
  31. assert samesite.lower() in [
  32. strict,
  33. lax,
  34. none,
  35. ], samesite must be either strict’, lax or none’”
  36. cookie[key][samesite] = samesite
  37. cookie_val = cookie.output(header=“”).strip()
  38. self.raw_headers.append((bset-cookie, cookie_val.encode(latin-1)))
  1. delete_cookie(
  2. key,
  3. path="/",
  4. domain=None,
  5. secure=False,
  6. httponly=False,
  7. samesite="lax",
  8. )
PARAMETERDESCRIPTION
key

TYPE: str

path

TYPE: str DEFAULT: ‘/‘

domain

TYPE: Optional[str] DEFAULT: None

secure

TYPE: bool DEFAULT: False

httponly

TYPE: bool DEFAULT: False

samesite

TYPE: Optional[Literal[‘lax’, ‘strict’, ‘none’]] DEFAULT: ‘lax’

Source code in starlette/responses.py

  1. 143
  2. 144
  3. 145
  4. 146
  5. 147
  6. 148
  7. 149
  8. 150
  9. 151
  10. 152
  11. 153
  12. 154
  13. 155
  14. 156
  15. 157
  16. 158
  17. 159
  18. 160
  19. 161
  1. def delete_cookie(
  2. self,
  3. key: str,
  4. path: str = “/“,
  5. domain: typing.Optional[str] = None,
  6. secure: bool = False,
  7. httponly: bool = False,
  8. samesite: typing.Optional[Literal[lax, strict, none]] = lax,
  9. ) -> None:
  10. self.set_cookie(
  11. key,
  12. max_age=0,
  13. expires=0,
  14. path=path,
  15. domain=domain,
  16. secure=secure,
  17. httponly=httponly,
  18. samesite=samesite,
  19. )

fastapi.responses.PlainTextResponse

  1. PlainTextResponse(
  2. content=None,
  3. status_code=200,
  4. headers=None,
  5. media_type=None,
  6. background=None,
  7. )

Bases: [Response](#fastapi.responses.Response "starlette.responses.Response")

PARAMETERDESCRIPTION
content

TYPE: Any DEFAULT: None

status_code

TYPE: int DEFAULT: 200

headers

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

media_type

TYPE: Optional[str] DEFAULT: None

background

TYPE: Optional[BackgroundTask] DEFAULT: None

Source code in starlette/responses.py

  1. 43
  2. 44
  3. 45
  4. 46
  5. 47
  6. 48
  7. 49
  8. 50
  9. 51
  10. 52
  11. 53
  12. 54
  13. 55
  14. 56
  1. def init(
  2. self,
  3. content: typing.Any = None,
  4. status_code: int = 200,
  5. headers: typing.Optional[typing.Mapping[str, str]] = None,
  6. media_type: typing.Optional[str] = None,
  7. background: typing.Optional[BackgroundTask] = None,
  8. ) -> None:
  9. self.status_code = status_code
  10. if media_type is not None:
  11. self.media_type = media_type
  12. self.background = background
  13. self.body = self.render(content)
  14. self.init_headers(headers)

charset class-attribute instance-attribute

  1. charset = 'utf-8'

status_code instance-attribute

  1. status_code = status_code

media_type class-attribute instance-attribute

  1. media_type = 'text/plain'

body instance-attribute

  1. body = render(content)

background instance-attribute

  1. background = background

headers property

  1. headers

render

  1. render(content)
PARAMETERDESCRIPTION
content

TYPE: Any

Source code in starlette/responses.py

  1. 58
  2. 59
  3. 60
  4. 61
  5. 62
  6. 63
  1. def render(self, content: typing.Any) -> bytes:
  2. if content is None:
  3. return b“”
  4. if isinstance(content, bytes):
  5. return content
  6. return content.encode(self.charset)

init_headers

  1. init_headers(headers=None)
PARAMETERDESCRIPTION
headers

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

Source code in starlette/responses.py

  1. 65
  2. 66
  3. 67
  4. 68
  5. 69
  6. 70
  7. 71
  8. 72
  9. 73
  10. 74
  11. 75
  12. 76
  13. 77
  14. 78
  15. 79
  16. 80
  17. 81
  18. 82
  19. 83
  20. 84
  21. 85
  22. 86
  23. 87
  24. 88
  25. 89
  26. 90
  27. 91
  28. 92
  29. 93
  30. 94
  31. 95
  32. 96
  1. def init_headers(
  2. self, headers: typing.Optional[typing.Mapping[str, str]] = None
  3. ) -> None:
  4. if headers is None:
  5. raw_headers: typing.List[typing.Tuple[bytes, bytes]] = []
  6. populate_content_length = True
  7. populate_content_type = True
  8. else:
  9. raw_headers = [
  10. (k.lower().encode(latin-1), v.encode(latin-1))
  11. for k, v in headers.items()
  12. ]
  13. keys = [h[0] for h in raw_headers]
  14. populate_content_length = bcontent-length not in keys
  15. populate_content_type = bcontent-type not in keys
  16. body = getattr(self, body, None)
  17. if (
  18. body is not None
  19. and populate_content_length
  20. and not (self.status_code < 200 or self.status_code in (204, 304))
  21. ):
  22. content_length = str(len(body))
  23. raw_headers.append((bcontent-length, content_length.encode(latin-1)))
  24. content_type = self.media_type
  25. if content_type is not None and populate_content_type:
  26. if content_type.startswith(text/“):
  27. content_type += “; charset=” + self.charset
  28. raw_headers.append((bcontent-type, content_type.encode(latin-1)))
  29. self.raw_headers = raw_headers
  1. set_cookie(
  2. key,
  3. value="",
  4. max_age=None,
  5. expires=None,
  6. path="/",
  7. domain=None,
  8. secure=False,
  9. httponly=False,
  10. samesite="lax",
  11. )
PARAMETERDESCRIPTION
key

TYPE: str

value

TYPE: str DEFAULT: ‘’

max_age

TYPE: Optional[int] DEFAULT: None

expires

TYPE: Optional[Union[datetime, str, int]] DEFAULT: None

path

TYPE: str DEFAULT: ‘/‘

domain

TYPE: Optional[str] DEFAULT: None

secure

TYPE: bool DEFAULT: False

httponly

TYPE: bool DEFAULT: False

samesite

TYPE: Optional[Literal[‘lax’, ‘strict’, ‘none’]] DEFAULT: ‘lax’

Source code in starlette/responses.py

  1. 104
  2. 105
  3. 106
  4. 107
  5. 108
  6. 109
  7. 110
  8. 111
  9. 112
  10. 113
  11. 114
  12. 115
  13. 116
  14. 117
  15. 118
  16. 119
  17. 120
  18. 121
  19. 122
  20. 123
  21. 124
  22. 125
  23. 126
  24. 127
  25. 128
  26. 129
  27. 130
  28. 131
  29. 132
  30. 133
  31. 134
  32. 135
  33. 136
  34. 137
  35. 138
  36. 139
  37. 140
  38. 141
  1. def set_cookie(
  2. self,
  3. key: str,
  4. value: str = “”,
  5. max_age: typing.Optional[int] = None,
  6. expires: typing.Optional[typing.Union[datetime, str, int]] = None,
  7. path: str = “/“,
  8. domain: typing.Optional[str] = None,
  9. secure: bool = False,
  10. httponly: bool = False,
  11. samesite: typing.Optional[Literal[lax, strict, none]] = lax,
  12. ) -> None:
  13. cookie: http.cookies.BaseCookie[str]” = http.cookies.SimpleCookie()
  14. cookie[key] = value
  15. if max_age is not None:
  16. cookie[key][max-age] = max_age
  17. if expires is not None:
  18. if isinstance(expires, datetime):
  19. cookie[key][expires] = format_datetime(expires, usegmt=True)
  20. else:
  21. cookie[key][expires] = expires
  22. if path is not None:
  23. cookie[key][path] = path
  24. if domain is not None:
  25. cookie[key][domain] = domain
  26. if secure:
  27. cookie[key][secure] = True
  28. if httponly:
  29. cookie[key][httponly] = True
  30. if samesite is not None:
  31. assert samesite.lower() in [
  32. strict,
  33. lax,
  34. none,
  35. ], samesite must be either strict’, lax or none’”
  36. cookie[key][samesite] = samesite
  37. cookie_val = cookie.output(header=“”).strip()
  38. self.raw_headers.append((bset-cookie, cookie_val.encode(latin-1)))
  1. delete_cookie(
  2. key,
  3. path="/",
  4. domain=None,
  5. secure=False,
  6. httponly=False,
  7. samesite="lax",
  8. )
PARAMETERDESCRIPTION
key

TYPE: str

path

TYPE: str DEFAULT: ‘/‘

domain

TYPE: Optional[str] DEFAULT: None

secure

TYPE: bool DEFAULT: False

httponly

TYPE: bool DEFAULT: False

samesite

TYPE: Optional[Literal[‘lax’, ‘strict’, ‘none’]] DEFAULT: ‘lax’

Source code in starlette/responses.py

  1. 143
  2. 144
  3. 145
  4. 146
  5. 147
  6. 148
  7. 149
  8. 150
  9. 151
  10. 152
  11. 153
  12. 154
  13. 155
  14. 156
  15. 157
  16. 158
  17. 159
  18. 160
  19. 161
  1. def delete_cookie(
  2. self,
  3. key: str,
  4. path: str = “/“,
  5. domain: typing.Optional[str] = None,
  6. secure: bool = False,
  7. httponly: bool = False,
  8. samesite: typing.Optional[Literal[lax, strict, none]] = lax,
  9. ) -> None:
  10. self.set_cookie(
  11. key,
  12. max_age=0,
  13. expires=0,
  14. path=path,
  15. domain=domain,
  16. secure=secure,
  17. httponly=httponly,
  18. samesite=samesite,
  19. )

fastapi.responses.RedirectResponse

  1. RedirectResponse(
  2. url, status_code=307, headers=None, background=None
  3. )

Bases: [Response](#fastapi.responses.Response "starlette.responses.Response")

PARAMETERDESCRIPTION
url

TYPE: Union[str, URL]

status_code

TYPE: int DEFAULT: 307

headers

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

background

TYPE: Optional[BackgroundTask] DEFAULT: None

Source code in starlette/responses.py

  1. 209
  2. 210
  3. 211
  4. 212
  5. 213
  6. 214
  7. 215
  8. 216
  9. 217
  10. 218
  11. 219
  1. def init(
  2. self,
  3. url: typing.Union[str, URL],
  4. statuscode: int = 307,
  5. headers: typing.Optional[typing.Mapping[str, str]] = None,
  6. background: typing.Optional[BackgroundTask] = None,
  7. ) -> None:
  8. super()._init(
  9. content=b“”, status_code=status_code, headers=headers, background=background
  10. )
  11. self.headers[location] = quote(str(url), safe=“:/%#?=@[]!$&’()*+,;”)

charset class-attribute instance-attribute

  1. charset = 'utf-8'

status_code instance-attribute

  1. status_code = status_code

media_type class-attribute instance-attribute

  1. media_type = None

body instance-attribute

  1. body = render(content)

background instance-attribute

  1. background = background

headers property

  1. headers

render

  1. render(content)
PARAMETERDESCRIPTION
content

TYPE: Any

Source code in starlette/responses.py

  1. 58
  2. 59
  3. 60
  4. 61
  5. 62
  6. 63
  1. def render(self, content: typing.Any) -> bytes:
  2. if content is None:
  3. return b“”
  4. if isinstance(content, bytes):
  5. return content
  6. return content.encode(self.charset)

init_headers

  1. init_headers(headers=None)
PARAMETERDESCRIPTION
headers

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

Source code in starlette/responses.py

  1. 65
  2. 66
  3. 67
  4. 68
  5. 69
  6. 70
  7. 71
  8. 72
  9. 73
  10. 74
  11. 75
  12. 76
  13. 77
  14. 78
  15. 79
  16. 80
  17. 81
  18. 82
  19. 83
  20. 84
  21. 85
  22. 86
  23. 87
  24. 88
  25. 89
  26. 90
  27. 91
  28. 92
  29. 93
  30. 94
  31. 95
  32. 96
  1. def init_headers(
  2. self, headers: typing.Optional[typing.Mapping[str, str]] = None
  3. ) -> None:
  4. if headers is None:
  5. raw_headers: typing.List[typing.Tuple[bytes, bytes]] = []
  6. populate_content_length = True
  7. populate_content_type = True
  8. else:
  9. raw_headers = [
  10. (k.lower().encode(latin-1), v.encode(latin-1))
  11. for k, v in headers.items()
  12. ]
  13. keys = [h[0] for h in raw_headers]
  14. populate_content_length = bcontent-length not in keys
  15. populate_content_type = bcontent-type not in keys
  16. body = getattr(self, body, None)
  17. if (
  18. body is not None
  19. and populate_content_length
  20. and not (self.status_code < 200 or self.status_code in (204, 304))
  21. ):
  22. content_length = str(len(body))
  23. raw_headers.append((bcontent-length, content_length.encode(latin-1)))
  24. content_type = self.media_type
  25. if content_type is not None and populate_content_type:
  26. if content_type.startswith(text/“):
  27. content_type += “; charset=” + self.charset
  28. raw_headers.append((bcontent-type, content_type.encode(latin-1)))
  29. self.raw_headers = raw_headers
  1. set_cookie(
  2. key,
  3. value="",
  4. max_age=None,
  5. expires=None,
  6. path="/",
  7. domain=None,
  8. secure=False,
  9. httponly=False,
  10. samesite="lax",
  11. )
PARAMETERDESCRIPTION
key

TYPE: str

value

TYPE: str DEFAULT: ‘’

max_age

TYPE: Optional[int] DEFAULT: None

expires

TYPE: Optional[Union[datetime, str, int]] DEFAULT: None

path

TYPE: str DEFAULT: ‘/‘

domain

TYPE: Optional[str] DEFAULT: None

secure

TYPE: bool DEFAULT: False

httponly

TYPE: bool DEFAULT: False

samesite

TYPE: Optional[Literal[‘lax’, ‘strict’, ‘none’]] DEFAULT: ‘lax’

Source code in starlette/responses.py

  1. 104
  2. 105
  3. 106
  4. 107
  5. 108
  6. 109
  7. 110
  8. 111
  9. 112
  10. 113
  11. 114
  12. 115
  13. 116
  14. 117
  15. 118
  16. 119
  17. 120
  18. 121
  19. 122
  20. 123
  21. 124
  22. 125
  23. 126
  24. 127
  25. 128
  26. 129
  27. 130
  28. 131
  29. 132
  30. 133
  31. 134
  32. 135
  33. 136
  34. 137
  35. 138
  36. 139
  37. 140
  38. 141
  1. def set_cookie(
  2. self,
  3. key: str,
  4. value: str = “”,
  5. max_age: typing.Optional[int] = None,
  6. expires: typing.Optional[typing.Union[datetime, str, int]] = None,
  7. path: str = “/“,
  8. domain: typing.Optional[str] = None,
  9. secure: bool = False,
  10. httponly: bool = False,
  11. samesite: typing.Optional[Literal[lax, strict, none]] = lax,
  12. ) -> None:
  13. cookie: http.cookies.BaseCookie[str]” = http.cookies.SimpleCookie()
  14. cookie[key] = value
  15. if max_age is not None:
  16. cookie[key][max-age] = max_age
  17. if expires is not None:
  18. if isinstance(expires, datetime):
  19. cookie[key][expires] = format_datetime(expires, usegmt=True)
  20. else:
  21. cookie[key][expires] = expires
  22. if path is not None:
  23. cookie[key][path] = path
  24. if domain is not None:
  25. cookie[key][domain] = domain
  26. if secure:
  27. cookie[key][secure] = True
  28. if httponly:
  29. cookie[key][httponly] = True
  30. if samesite is not None:
  31. assert samesite.lower() in [
  32. strict,
  33. lax,
  34. none,
  35. ], samesite must be either strict’, lax or none’”
  36. cookie[key][samesite] = samesite
  37. cookie_val = cookie.output(header=“”).strip()
  38. self.raw_headers.append((bset-cookie, cookie_val.encode(latin-1)))
  1. delete_cookie(
  2. key,
  3. path="/",
  4. domain=None,
  5. secure=False,
  6. httponly=False,
  7. samesite="lax",
  8. )
PARAMETERDESCRIPTION
key

TYPE: str

path

TYPE: str DEFAULT: ‘/‘

domain

TYPE: Optional[str] DEFAULT: None

secure

TYPE: bool DEFAULT: False

httponly

TYPE: bool DEFAULT: False

samesite

TYPE: Optional[Literal[‘lax’, ‘strict’, ‘none’]] DEFAULT: ‘lax’

Source code in starlette/responses.py

  1. 143
  2. 144
  3. 145
  4. 146
  5. 147
  6. 148
  7. 149
  8. 150
  9. 151
  10. 152
  11. 153
  12. 154
  13. 155
  14. 156
  15. 157
  16. 158
  17. 159
  18. 160
  19. 161
  1. def delete_cookie(
  2. self,
  3. key: str,
  4. path: str = “/“,
  5. domain: typing.Optional[str] = None,
  6. secure: bool = False,
  7. httponly: bool = False,
  8. samesite: typing.Optional[Literal[lax, strict, none]] = lax,
  9. ) -> None:
  10. self.set_cookie(
  11. key,
  12. max_age=0,
  13. expires=0,
  14. path=path,
  15. domain=domain,
  16. secure=secure,
  17. httponly=httponly,
  18. samesite=samesite,
  19. )

fastapi.responses.Response

  1. Response(
  2. content=None,
  3. status_code=200,
  4. headers=None,
  5. media_type=None,
  6. background=None,
  7. )
PARAMETERDESCRIPTION
content

TYPE: Any DEFAULT: None

status_code

TYPE: int DEFAULT: 200

headers

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

media_type

TYPE: Optional[str] DEFAULT: None

background

TYPE: Optional[BackgroundTask] DEFAULT: None

Source code in starlette/responses.py

  1. 43
  2. 44
  3. 45
  4. 46
  5. 47
  6. 48
  7. 49
  8. 50
  9. 51
  10. 52
  11. 53
  12. 54
  13. 55
  14. 56
  1. def init(
  2. self,
  3. content: typing.Any = None,
  4. status_code: int = 200,
  5. headers: typing.Optional[typing.Mapping[str, str]] = None,
  6. media_type: typing.Optional[str] = None,
  7. background: typing.Optional[BackgroundTask] = None,
  8. ) -> None:
  9. self.status_code = status_code
  10. if media_type is not None:
  11. self.media_type = media_type
  12. self.background = background
  13. self.body = self.render(content)
  14. self.init_headers(headers)

charset class-attribute instance-attribute

  1. charset = 'utf-8'

status_code instance-attribute

  1. status_code = status_code

media_type class-attribute instance-attribute

  1. media_type = None

body instance-attribute

  1. body = render(content)

background instance-attribute

  1. background = background

headers property

  1. headers

render

  1. render(content)
PARAMETERDESCRIPTION
content

TYPE: Any

Source code in starlette/responses.py

  1. 58
  2. 59
  3. 60
  4. 61
  5. 62
  6. 63
  1. def render(self, content: typing.Any) -> bytes:
  2. if content is None:
  3. return b“”
  4. if isinstance(content, bytes):
  5. return content
  6. return content.encode(self.charset)

init_headers

  1. init_headers(headers=None)
PARAMETERDESCRIPTION
headers

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

Source code in starlette/responses.py

  1. 65
  2. 66
  3. 67
  4. 68
  5. 69
  6. 70
  7. 71
  8. 72
  9. 73
  10. 74
  11. 75
  12. 76
  13. 77
  14. 78
  15. 79
  16. 80
  17. 81
  18. 82
  19. 83
  20. 84
  21. 85
  22. 86
  23. 87
  24. 88
  25. 89
  26. 90
  27. 91
  28. 92
  29. 93
  30. 94
  31. 95
  32. 96
  1. def init_headers(
  2. self, headers: typing.Optional[typing.Mapping[str, str]] = None
  3. ) -> None:
  4. if headers is None:
  5. raw_headers: typing.List[typing.Tuple[bytes, bytes]] = []
  6. populate_content_length = True
  7. populate_content_type = True
  8. else:
  9. raw_headers = [
  10. (k.lower().encode(latin-1), v.encode(latin-1))
  11. for k, v in headers.items()
  12. ]
  13. keys = [h[0] for h in raw_headers]
  14. populate_content_length = bcontent-length not in keys
  15. populate_content_type = bcontent-type not in keys
  16. body = getattr(self, body, None)
  17. if (
  18. body is not None
  19. and populate_content_length
  20. and not (self.status_code < 200 or self.status_code in (204, 304))
  21. ):
  22. content_length = str(len(body))
  23. raw_headers.append((bcontent-length, content_length.encode(latin-1)))
  24. content_type = self.media_type
  25. if content_type is not None and populate_content_type:
  26. if content_type.startswith(text/“):
  27. content_type += “; charset=” + self.charset
  28. raw_headers.append((bcontent-type, content_type.encode(latin-1)))
  29. self.raw_headers = raw_headers
  1. set_cookie(
  2. key,
  3. value="",
  4. max_age=None,
  5. expires=None,
  6. path="/",
  7. domain=None,
  8. secure=False,
  9. httponly=False,
  10. samesite="lax",
  11. )
PARAMETERDESCRIPTION
key

TYPE: str

value

TYPE: str DEFAULT: ‘’

max_age

TYPE: Optional[int] DEFAULT: None

expires

TYPE: Optional[Union[datetime, str, int]] DEFAULT: None

path

TYPE: str DEFAULT: ‘/‘

domain

TYPE: Optional[str] DEFAULT: None

secure

TYPE: bool DEFAULT: False

httponly

TYPE: bool DEFAULT: False

samesite

TYPE: Optional[Literal[‘lax’, ‘strict’, ‘none’]] DEFAULT: ‘lax’

Source code in starlette/responses.py

  1. 104
  2. 105
  3. 106
  4. 107
  5. 108
  6. 109
  7. 110
  8. 111
  9. 112
  10. 113
  11. 114
  12. 115
  13. 116
  14. 117
  15. 118
  16. 119
  17. 120
  18. 121
  19. 122
  20. 123
  21. 124
  22. 125
  23. 126
  24. 127
  25. 128
  26. 129
  27. 130
  28. 131
  29. 132
  30. 133
  31. 134
  32. 135
  33. 136
  34. 137
  35. 138
  36. 139
  37. 140
  38. 141
  1. def set_cookie(
  2. self,
  3. key: str,
  4. value: str = “”,
  5. max_age: typing.Optional[int] = None,
  6. expires: typing.Optional[typing.Union[datetime, str, int]] = None,
  7. path: str = “/“,
  8. domain: typing.Optional[str] = None,
  9. secure: bool = False,
  10. httponly: bool = False,
  11. samesite: typing.Optional[Literal[lax, strict, none]] = lax,
  12. ) -> None:
  13. cookie: http.cookies.BaseCookie[str]” = http.cookies.SimpleCookie()
  14. cookie[key] = value
  15. if max_age is not None:
  16. cookie[key][max-age] = max_age
  17. if expires is not None:
  18. if isinstance(expires, datetime):
  19. cookie[key][expires] = format_datetime(expires, usegmt=True)
  20. else:
  21. cookie[key][expires] = expires
  22. if path is not None:
  23. cookie[key][path] = path
  24. if domain is not None:
  25. cookie[key][domain] = domain
  26. if secure:
  27. cookie[key][secure] = True
  28. if httponly:
  29. cookie[key][httponly] = True
  30. if samesite is not None:
  31. assert samesite.lower() in [
  32. strict,
  33. lax,
  34. none,
  35. ], samesite must be either strict’, lax or none’”
  36. cookie[key][samesite] = samesite
  37. cookie_val = cookie.output(header=“”).strip()
  38. self.raw_headers.append((bset-cookie, cookie_val.encode(latin-1)))
delete_cookie(
    key,
    path="/",
    domain=None,
    secure=False,
    httponly=False,
    samesite="lax",
)
PARAMETERDESCRIPTION
key

TYPE: str

path

TYPE: str DEFAULT: ‘/‘

domain

TYPE: Optional[str] DEFAULT: None

secure

TYPE: bool DEFAULT: False

httponly

TYPE: bool DEFAULT: False

samesite

TYPE: Optional[Literal[‘lax’, ‘strict’, ‘none’]] DEFAULT: ‘lax’

Source code in starlette/responses.py

143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
def delete_cookie(
    self,
    key: str,
    path: str = “/“,
    domain: typing.Optional[str] = None,
    secure: bool = False,
    httponly: bool = False,
    samesite: typing.Optional[Literal[“lax”, “strict”, “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(
    content,
    status_code=200,
    headers=None,
    media_type=None,
    background=None,
)

Bases: [Response](#fastapi.responses.Response "starlette.responses.Response")

PARAMETERDESCRIPTION
content

TYPE: ContentStream

status_code

TYPE: int DEFAULT: 200

headers

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

media_type

TYPE: Optional[str] DEFAULT: None

background

TYPE: Optional[BackgroundTask] DEFAULT: None

Source code in starlette/responses.py

231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
def init(
    self,
    content: ContentStream,
    status_code: int = 200,
    headers: typing.Optional[typing.Mapping[str, str]] = None,
    media_type: typing.Optional[str] = None,
    background: typing.Optional[BackgroundTask] = 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(content)
PARAMETERDESCRIPTION
content

TYPE: Any

Source code in starlette/responses.py

58
59
60
61
62
63
def render(self, content: typing.Any) -> bytes:
    if content is None:
        return b“”
    if isinstance(content, bytes):
        return content
    return content.encode(self.charset)

init_headers

init_headers(headers=None)
PARAMETERDESCRIPTION
headers

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

Source code in starlette/responses.py

65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
def init_headers(
    self, headers: typing.Optional[typing.Mapping[str, str]] = None
) -> None:
    if headers is None:
        raw_headers: typing.List[typing.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/“):
            content_type += “; charset=” + self.charset
        raw_headers.append((b“content-type”, content_type.encode(“latin-1”)))

    self.raw_headers = raw_headers
set_cookie(
    key,
    value="",
    max_age=None,
    expires=None,
    path="/",
    domain=None,
    secure=False,
    httponly=False,
    samesite="lax",
)
PARAMETERDESCRIPTION
key

TYPE: str

value

TYPE: str DEFAULT: ‘’

max_age

TYPE: Optional[int] DEFAULT: None

expires

TYPE: Optional[Union[datetime, str, int]] DEFAULT: None

path

TYPE: str DEFAULT: ‘/‘

domain

TYPE: Optional[str] DEFAULT: None

secure

TYPE: bool DEFAULT: False

httponly

TYPE: bool DEFAULT: False

samesite

TYPE: Optional[Literal[‘lax’, ‘strict’, ‘none’]] DEFAULT: ‘lax’

Source code in starlette/responses.py

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
129
130
131
132
133
134
135
136
137
138
139
140
141
def set_cookie(
    self,
    key: str,
    value: str = “”,
    max_age: typing.Optional[int] = None,
    expires: typing.Optional[typing.Union[datetime, str, int]] = None,
    path: str = “/“,
    domain: typing.Optional[str] = None,
    secure: bool = False,
    httponly: bool = False,
    samesite: typing.Optional[Literal[“lax”, “strict”, “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(
    key,
    path="/",
    domain=None,
    secure=False,
    httponly=False,
    samesite="lax",
)
PARAMETERDESCRIPTION
key

TYPE: str

path

TYPE: str DEFAULT: ‘/‘

domain

TYPE: Optional[str] DEFAULT: None

secure

TYPE: bool DEFAULT: False

httponly

TYPE: bool DEFAULT: False

samesite

TYPE: Optional[Literal[‘lax’, ‘strict’, ‘none’]] DEFAULT: ‘lax’

Source code in starlette/responses.py

143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
def delete_cookie(
    self,
    key: str,
    path: str = “/“,
    domain: typing.Optional[str] = None,
    secure: bool = False,
    httponly: bool = False,
    samesite: typing.Optional[Literal[“lax”, “strict”, “none”]] = “lax”,
) -> None:
    self.set_cookie(
        key,
        max_age=0,
        expires=0,
        path=path,
        domain=domain,
        secure=secure,
        httponly=httponly,
        samesite=samesite,
    )