Request class

You can declare a parameter in a path operation function or dependency to be of type Request and then you can access the raw request object directly, without any validation, etc.

You can import it directly from fastapi:

  1. from fastapi import Request

Tip

When you want to define dependencies that should be compatible with both HTTP and WebSockets, you can define a parameter that takes an HTTPConnection instead of a Request or a WebSocket.

fastapi.Request

  1. Request(scope, receive=empty_receive, send=empty_send)

Bases: [HTTPConnection](https:/fastapi.tiangolo.com/zh/reference/httpconnection/#fastapi.requests.HTTPConnection "starlette.requests.HTTPConnection")

PARAMETERDESCRIPTION
scope

TYPE: Scope

receive

TYPE: Receive DEFAULT: empty_receive

send

TYPE: Send DEFAULT: empty_send

Source code in starlette/requests.py

  1. 193
  2. 194
  3. 195
  4. 196
  5. 197
  6. 198
  7. 199
  8. 200
  9. 201
  10. 202
  1. def init(
  2. self, scope: Scope, receive: Receive = emptyreceive, send: Send = emptysend
  3. ):
  4. super().__init(scope)
  5. assert scope[type] == http
  6. self._receive = receive
  7. self._send = send
  8. self._stream_consumed = False
  9. self._is_disconnected = False
  10. self._form = None

scope instance-attribute

  1. scope = scope

app property

  1. app

url property

  1. url

base_url property

  1. base_url

headers property

  1. headers

query_params property

  1. query_params

path_params property

  1. path_params

cookies property

  1. cookies

client property

  1. client

session property

  1. session

auth property

  1. auth

user property

  1. user

state property

  1. state

method property

  1. method

receive property

  1. receive

url_for

  1. url_for(__name, **path_params)
PARAMETERDESCRIPTION
__name

TYPE: str

**path_params

TYPE: Any DEFAULT: {}

Source code in starlette/requests.py

  1. 176
  2. 177
  3. 178
  4. 179
  1. def url_for(self, name: str, **path_params: typing.Any) -> URL:
  2. router: Router = self.scope[router]
  3. url_path = router.url_path_for(name, **path_params)
  4. return url_path.make_absolute_url(base_url=self.base_url)

stream async

  1. stream()

Source code in starlette/requests.py

  1. 212
  2. 213
  3. 214
  4. 215
  5. 216
  6. 217
  7. 218
  8. 219
  9. 220
  10. 221
  11. 222
  12. 223
  13. 224
  14. 225
  15. 226
  16. 227
  17. 228
  18. 229
  19. 230
  20. 231
  1. async def stream(self) -> typing.AsyncGenerator[bytes, None]:
  2. if hasattr(self, _body):
  3. yield self._body
  4. yield b“”
  5. return
  6. if self._stream_consumed:
  7. raise RuntimeError(Stream consumed)
  8. self._stream_consumed = True
  9. while True:
  10. message = await self._receive()
  11. if message[type] == http.request:
  12. body = message.get(body, b“”)
  13. if body:
  14. yield body
  15. if not message.get(more_body, False):
  16. break
  17. elif message[type] == http.disconnect:
  18. self._is_disconnected = True
  19. raise ClientDisconnect()
  20. yield b“”

body async

  1. body()

Source code in starlette/requests.py

  1. 233
  2. 234
  3. 235
  4. 236
  5. 237
  6. 238
  7. 239
  1. async def body(self) -> bytes:
  2. if not hasattr(self, _body):
  3. chunks: typing.List[bytes]” = []
  4. async for chunk in self.stream():
  5. chunks.append(chunk)
  6. self._body = b“”.join(chunks)
  7. return self._body

json async

  1. json()

Source code in starlette/requests.py

  1. 241
  2. 242
  3. 243
  4. 244
  5. 245
  1. async def json(self) -> typing.Any:
  2. if not hasattr(self, _json):
  3. body = await self.body()
  4. self._json = json.loads(body)
  5. return self._json

form

  1. form(*, max_files=1000, max_fields=1000)
PARAMETERDESCRIPTION
max_files

TYPE: Union[int, float] DEFAULT: 1000

max_fields

TYPE: Union[int, float] DEFAULT: 1000

Source code in starlette/requests.py

  1. 280
  2. 281
  3. 282
  4. 283
  5. 284
  6. 285
  7. 286
  8. 287
  9. 288
  1. def form(
  2. self,
  3. *,
  4. max_files: typing.Union[int, float] = 1000,
  5. max_fields: typing.Union[int, float] = 1000,
  6. ) -> AwaitableOrContextManager[FormData]:
  7. return AwaitableOrContextManagerWrapper(
  8. self._get_form(max_files=max_files, max_fields=max_fields)
  9. )

close async

  1. close()

Source code in starlette/requests.py

  1. 290
  2. 291
  3. 292
  1. async def close(self) -> None:
  2. if self._form is not None:
  3. await self._form.close()

is_disconnected async

  1. is_disconnected()

Source code in starlette/requests.py

  1. 294
  2. 295
  3. 296
  4. 297
  5. 298
  6. 299
  7. 300
  8. 301
  9. 302
  10. 303
  11. 304
  12. 305
  13. 306
  1. async def is_disconnected(self) -> bool:
  2. if not self._is_disconnected:
  3. message: Message = {}
  4. # If message isn’t immediately available, move on
  5. with anyio.CancelScope() as cs:
  6. cs.cancel()
  7. message = await self._receive()
  8. if message.get(type) == http.disconnect:
  9. self._is_disconnected = True
  10. return self._is_disconnected

send_push_promise async

  1. send_push_promise(path)
PARAMETERDESCRIPTION
path

TYPE: str

Source code in starlette/requests.py

  1. 308
  2. 309
  3. 310
  4. 311
  5. 312
  6. 313
  7. 314
  8. 315
  9. 316
  10. 317
  11. 318
  1. async def send_push_promise(self, path: str) -> None:
  2. if http.response.push in self.scope.get(extensions, {}):
  3. raw_headers: typing.List[typing.Tuple[bytes, bytes]]” = []
  4. for name in SERVER_PUSH_HEADERS_TO_COPY:
  5. for value in self.headers.getlist(name):
  6. raw_headers.append(
  7. (name.encode(latin-1), value.encode(latin-1))
  8. )
  9. await self._send(
  10. {type: http.response.push, path: path, headers: raw_headers}
  11. )