Response class

You can declare a parameter in a path operation function or dependency to be of type Response and then you can set data for the response like headers or cookies.

You can also use it directly to create an instance of it and return it from your path operations.

You can import it directly from fastapi:

  1. from fastapi import Response

fastapi.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)

media_type class-attribute instance-attribute

  1. media_type = None

charset class-attribute instance-attribute

  1. charset = 'utf-8'

status_code instance-attribute

  1. status_code = status_code

background instance-attribute

  1. background = background

body instance-attribute

  1. body = render(content)

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. )