コンテンツへスキップ

Request クラス

パス操作関数 または依存関係で Request 型のパラメーターを宣言すると、検証なしで生のリクエストオブジェクトに直接アクセスできます。

fastapiから直接インポートできます。

from fastapi import Request

ヒント

HTTPとWebSocketの両方と互換性があるべき依存関係を定義したい場合、RequestWebSocketの代わりにHTTPConnectionを受け取るパラメータを定義できます。

fastapi.Request

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

ベース: HTTPConnection

starlette/requests.py内のソースコード
201
202
203
204
205
206
207
208
def __init__(self, scope: Scope, receive: Receive = empty_receive, send: Send = empty_send):
    super().__init__(scope)
    assert scope["type"] == "http"
    self._receive = receive
    self._send = send
    self._stream_consumed = False
    self._is_disconnected = False
    self._form = None

scope インスタンス属性

scope = scope

app プロパティ

app

url プロパティ

url

base_url プロパティ

base_url

headers プロパティ

headers

query_params プロパティ

query_params

path_params プロパティ

path_params

cookies プロパティ

cookies

client プロパティ

client

session プロパティ

session

auth プロパティ

auth

user プロパティ

user

state プロパティ

state

method プロパティ

method

receive プロパティ

receive

url_for

url_for(name, /, **path_params)
starlette/requests.py内のソースコード
182
183
184
185
186
187
def url_for(self, name: str, /, **path_params: typing.Any) -> URL:
    url_path_provider: Router | Starlette | None = self.scope.get("router") or self.scope.get("app")
    if url_path_provider is None:
        raise RuntimeError("The `url_for` method can only be used inside a Starlette application or with a router.")
    url_path = url_path_provider.url_path_for(name, **path_params)
    return url_path.make_absolute_url(base_url=self.base_url)

stream async

stream()
starlette/requests.py内のソースコード
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
async def stream(self) -> typing.AsyncGenerator[bytes, None]:
    if hasattr(self, "_body"):
        yield self._body
        yield b""
        return
    if self._stream_consumed:
        raise RuntimeError("Stream consumed")
    while not self._stream_consumed:
        message = await self._receive()
        if message["type"] == "http.request":
            body = message.get("body", b"")
            if not message.get("more_body", False):
                self._stream_consumed = True
            if body:
                yield body
        elif message["type"] == "http.disconnect":  # pragma: no branch
            self._is_disconnected = True
            raise ClientDisconnect()
    yield b""

body async

body()
starlette/requests.py内のソースコード
238
239
240
241
242
243
244
async def body(self) -> bytes:
    if not hasattr(self, "_body"):
        chunks: list[bytes] = []
        async for chunk in self.stream():
            chunks.append(chunk)
        self._body = b"".join(chunks)
    return self._body

json async

json()
starlette/requests.py内のソースコード
246
247
248
249
250
async def json(self) -> typing.Any:
    if not hasattr(self, "_json"):  # pragma: no branch
        body = await self.body()
        self._json = json.loads(body)
    return self._json

form

form(
    *,
    max_files=1000,
    max_fields=1000,
    max_part_size=1024 * 1024
)
starlette/requests.py内のソースコード
287
288
289
290
291
292
293
294
295
296
def form(
    self,
    *,
    max_files: int | float = 1000,
    max_fields: int | float = 1000,
    max_part_size: int = 1024 * 1024,
) -> AwaitableOrContextManager[FormData]:
    return AwaitableOrContextManagerWrapper(
        self._get_form(max_files=max_files, max_fields=max_fields, max_part_size=max_part_size)
    )

close async

close()
starlette/requests.py内のソースコード
298
299
300
async def close(self) -> None:
    if self._form is not None:  # pragma: no branch
        await self._form.close()

is_disconnected async

is_disconnected()
starlette/requests.py内のソースコード
302
303
304
305
306
307
308
309
310
311
312
313
314
async def is_disconnected(self) -> bool:
    if not self._is_disconnected:
        message: Message = {}

        # If message isn't immediately available, move on
        with anyio.CancelScope() as cs:
            cs.cancel()
            message = await self._receive()

        if message.get("type") == "http.disconnect":
            self._is_disconnected = True

    return self._is_disconnected

send_push_promise async

send_push_promise(path)
starlette/requests.py内のソースコード
316
317
318
319
320
321
322
async def send_push_promise(self, path: str) -> None:
    if "http.response.push" in self.scope.get("extensions", {}):
        raw_headers: list[tuple[bytes, bytes]] = []
        for name in SERVER_PUSH_HEADERS_TO_COPY:
            for value in self.headers.getlist(name):
                raw_headers.append((name.encode("latin-1"), value.encode("latin-1")))
        await self._send({"type": "http.response.push", "path": path, "headers": raw_headers})