Movatterモバイル変換


[0]ホーム

URL:


Skip to content
Join theFastAPI Cloud waiting list 🚀
Follow@fastapi onX (Twitter) to stay updated
FollowFastAPI onLinkedIn to stay updated
Subscribe to theFastAPI and friends newsletter 🎉
sponsor
sponsor
sponsor
sponsor
sponsor
sponsor
sponsor
sponsor
sponsor
sponsor
sponsor

Request class

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

Read more about it in theFastAPI docs about using Request directly

You can import it directly fromfastapi:

fromfastapiimportRequest

Tip

When you want to define dependencies that should be compatible with both HTTP and WebSockets, you can define a parameter that takes anHTTPConnection instead of aRequest or aWebSocket.

fastapi.Request

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

Bases:HTTPConnection[StateT]

Source code instarlette/requests.py
211212213214215216217218
def__init__(self,scope:Scope,receive:Receive=empty_receive,send:Send=empty_send):super().__init__(scope)assertscope["type"]=="http"self._receive=receiveself._send=sendself._stream_consumed=Falseself._is_disconnected=Falseself._form=None

scopeinstance-attribute

scope=scope

appproperty

app

urlproperty

url

base_urlproperty

base_url

headersproperty

headers

query_paramsproperty

query_params

path_paramsproperty

path_params

cookiesproperty

cookies

clientproperty

client

sessionproperty

session

authproperty

auth

userproperty

user

stateproperty

state

methodproperty

method

receiveproperty

receive

url_for

url_for(name,/,**path_params)
Source code instarlette/requests.py
192193194195196197
defurl_for(self,name:str,/,**path_params:Any)->URL:url_path_provider:Router|Starlette|None=self.scope.get("router")orself.scope.get("app")ifurl_path_providerisNone:raiseRuntimeError("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)returnurl_path.make_absolute_url(base_url=self.base_url)

streamasync

stream()
Source code instarlette/requests.py
228229230231232233234235236237238239240241242243244245246
asyncdefstream(self)->AsyncGenerator[bytes,None]:ifhasattr(self,"_body"):yieldself._bodyyieldb""returnifself._stream_consumed:raiseRuntimeError("Stream consumed")whilenotself._stream_consumed:message=awaitself._receive()ifmessage["type"]=="http.request":body=message.get("body",b"")ifnotmessage.get("more_body",False):self._stream_consumed=Trueifbody:yieldbodyelifmessage["type"]=="http.disconnect":# pragma: no branchself._is_disconnected=TrueraiseClientDisconnect()yieldb""

bodyasync

body()
Source code instarlette/requests.py
248249250251252253254
asyncdefbody(self)->bytes:ifnothasattr(self,"_body"):chunks:list[bytes]=[]asyncforchunkinself.stream():chunks.append(chunk)self._body=b"".join(chunks)returnself._body

jsonasync

json()
Source code instarlette/requests.py
256257258259260
asyncdefjson(self)->Any:ifnothasattr(self,"_json"):# pragma: no branchbody=awaitself.body()self._json=json.loads(body)returnself._json

form

form(*,max_files=1000,max_fields=1000,max_part_size=1024*1024)
Source code instarlette/requests.py
297298299300301302303304305306
defform(self,*,max_files:int|float=1000,max_fields:int|float=1000,max_part_size:int=1024*1024,)->AwaitableOrContextManager[FormData]:returnAwaitableOrContextManagerWrapper(self._get_form(max_files=max_files,max_fields=max_fields,max_part_size=max_part_size))

closeasync

close()
Source code instarlette/requests.py
308309310
asyncdefclose(self)->None:ifself._formisnotNone:# pragma: no branchawaitself._form.close()

is_disconnectedasync

is_disconnected()
Source code instarlette/requests.py
312313314315316317318319320321322323324
asyncdefis_disconnected(self)->bool:ifnotself._is_disconnected:message:Message={}# If message isn't immediately available, move onwithanyio.CancelScope()ascs:cs.cancel()message=awaitself._receive()ifmessage.get("type")=="http.disconnect":self._is_disconnected=Truereturnself._is_disconnected

send_push_promiseasync

send_push_promise(path)
Source code instarlette/requests.py
326327328329330331332
asyncdefsend_push_promise(self,path:str)->None:if"http.response.push"inself.scope.get("extensions",{}):raw_headers:list[tuple[bytes,bytes]]=[]fornameinSERVER_PUSH_HEADERS_TO_COPY:forvalueinself.headers.getlist(name):raw_headers.append((name.encode("latin-1"),value.encode("latin-1")))awaitself._send({"type":"http.response.push","path":path,"headers":raw_headers})

[8]ページ先頭

©2009-2026 Movatter.jp