コンテンツにスキップ
per page, with , order by , clip by
Results of 1 - 10 of about 50 for response (0.003 sec.)
テスト 11024
テスト Starlette のおかげで、 FastAPI アプリケーションのテストは簡単で楽しいものになっています。 HTTPX がベースなので、非常に使いやすく直感的です。 これを使用すると、 FastAPI と共に pytest を直接利用できます。 TestClient を使用 TestClient を ... . get ( "/" ) async def read_main (): return { "msg" : "Hello World" } client = TestClient ( app ) def test_read_main (): response = client . get ( "/" ) assert response . status_code == 200 assert response . json () == { "msg" : "Hello World" } 豆知識 ... on 3.8+ from fastapi.testclient import TestClient from .main import app client = TestClient ( app ) def test_read_main (): response = client . get ( "/" ) assert response . status_code == 200 assert response . json () == { "msg" : "Hello World" } テスト: ... tAPI () class Item ( BaseModel ): id : str title : str description : str | None = None @app . get ( "/items/ {item_id} " , response_model = Item ) async def read_main ( item_id : str , x_token : str = Header ()): if x_token != fake_secret_token : raise H ...
https://man.mtool.work/fastapi/0.115/tutorial/testing/
依存関係としてのクラス 10572
依存関係としてのクラス 依存性注入 システムを深く掘り下げる前に、先ほどの例をアップグレードしてみましょう。 前の例の dict 前の例では、依存関係("dependable")から dict を返していました: Python 3.8+ - non-Annotated from typing import Union fr ... imit = limit @app . get ( "/items/" ) async def read_items ( commons : CommonQueryParams = Depends ( CommonQueryParams )): response = {} if commons . q : response . update ({ "q" : commons . q }) items = fake_items_db [ commons . skip : commons . skip + ... commons . limit ] response . update ({ "items" : items }) return response ? Other versions and variants Python 3.10+ Python 3.9+ Python 3.8+ Python 3 ... @app . get ( "/items/" ) async def read_items ( commons : Annotated [ CommonQueryParams , Depends ( CommonQueryParams )]): response = {} if commons . q : response . update ({ "q" : commons . q }) items = fake_items_db [ commons . skip : commons . skip + ...
https://man.mtool.work/fastapi/0.115/tutorial/dependencies/classes-as-dependenci...
Response Cookies 10377
Response Cookies Warning The current page still doesn't have a translation for this language. But you can help translating it: Contributing . Use a Response parameter You can declare a parameter of type Response in your path operation funct ... ion . And then you can set cookies in that temporal response object. Python 3.8+ from fastapi import FastAPI , Response app = FastAPI () @app . post ( "/cookie-and-object/" ) def crea ... te_cookie ( response : Response ): response . set_cookie ( key = "fakesession" , value = "fake-cookie-session-value" ) return { "message" : "Co ... And then you can return any object you need, as you normally would (a dict , a database model, etc). And if you declared a response_model , it will still be used to filter and convert the object you returned. FastAPI will use that temporal response to ex ...
https://man.mtool.work/fastapi/0.115/advanced/response-cookies/
Testing Dependencies with Overrides 10091
Testing Dependencies with Overrides Warning The current page still doesn't have a translation for this language. But you can help translating it: Contributing . Overriding dependencies during testing There are some scenarios where you might ... : 5 , "limit" : 10 } app . dependency_overrides [ common_parameters ] = override_dependency def test_override_in_items (): response = client . get ( "/items/" ) assert response . status_code == 200 assert response . json () == { "message" : "Hello Items! ... " , "params" : { "q" : None , "skip" : 5 , "limit" : 10 }, } def test_override_in_items_with_q (): response = client . get ( "/items/?q=foo" ) assert response . status_code == 200 assert response . json () == { "message" : "Hello ... Items!" , "params" : { "q" : "foo" , "skip" : 5 , "limit" : 10 }, } def test_override_in_items_with_params (): response = client . get ( "/items/?q=foo&skip=100&limit=200" ) assert response . status_code == 200 assert response . json () == { ...
https://man.mtool.work/fastapi/0.115/advanced/testing-dependencies/
Response Headers 9986
Response Headers Warning The current page still doesn't have a translation for this language. But you can help translating it: Contributing . Use a Response parameter You can declare a parameter of type Response in your path operation funct ... ion (as you can do for cookies). And then you can set headers in that temporal response object. Python 3.8+ from fastapi import FastAPI , Response app = FastAPI () @app . get ( "/headers-and-object/" ) def get_ ... headers ( response : Response ): response . headers [ "X-Cat-Dog" ] = "alone in the world" return { "message" : "Hello World" } And then you ... can return any object you need, as you normally would (a dict , a database model, etc). And if you declared a response_model , it will still be used to filter and convert the object you returned. FastAPI will use that temporal response to ex ...
https://man.mtool.work/fastapi/0.115/advanced/response-headers/
Custom Request and APIRoute class 9896
Custom Request and APIRoute class Warning The current page still doesn't have a translation for this language. But you can help translating it: Contributing . In some cases, you may want to override the logic used by the Request and APIRout ... ressed requests. Python 3.8+ import gzip from typing import Callable , List from fastapi import Body , FastAPI , Request , Response from fastapi.routing import APIRoute class GzipRequest ( Request ): async def body ( self ) -> bytes : if not hasattr ( se ... allable : original_route_handler = super () . get_route_handler () async def custom_route_handler ( request : Request ) -> Response : request = GzipRequest ( request . scope , request . receive ) return await original_route_handler ( request ) return cus ... IRoute.get_route_handler() . This method returns a function. And that function is what will receive a request and return a response. Here we use it to create a GzipRequest from the original request. Python 3.8+ import gzip from typing import Callable , L ...
https://man.mtool.work/fastapi/0.115/how-to/custom-request-and-route/
レスポンスモデル 9866
レスポンスモデル path operations のいずれにおいても、 response_model パラメータを使用して、レスポンスのモデルを宣言することができます: @app.get() @app.post() @app.put() @app.delete() など。 Python 3.8+ from typing import Any , List , Union ... [ str , None ] = None price : float tax : Union [ float , None ] = None tags : List [ str ] = [] @app . post ( "/items/" , response_model = Item ) async def create_item ( item : Item ) -> Any : return item @app . get ( "/items/" , response_model = List [ ... description : str | None = None price : float tax : float | None = None tags : list [ str ] = [] @app . post ( "/items/" , response_model = Item ) async def create_item ( item : Item ) -> Any : return item @app . get ( "/items/" , response_model = list [ ... [ str , None ] = None price : float tax : Union [ float , None ] = None tags : list [ str ] = [] @app . post ( "/items/" , response_model = Item ) async def create_item ( item : Item ) -> Any : return item @app . get ( "/items/" , response_model = list [ ...
https://man.mtool.work/fastapi/0.115/tutorial/response-model/
Response - Change Status Code 9790
Response - Change Status Code Warning The current page still doesn't have a translation for this language. But you can help translating it: Contributing . You probably read before that you can set a default Response Status Code . But in som ... turn an HTTP status code of "CREATED" 201 . But you still want to be able to filter and convert the data you return with a response_model . For those cases, you can use a Response parameter. Use a Response parameter You can declare a parameter of type Re ... our path operation function (as you can do for cookies and headers). And then you can set the status_code in that temporal response object. Python 3.8+ from fastapi import FastAPI , Response , status app = FastAPI () tasks = { "foo" : "Listen to the Bar ... Fighters" } @app . put ( "/get-or-create-task/ {task_id} " , status_code = 200 ) def get_or_create_task ( task_id : str , response : Response ): if task_id not in tasks : tasks [ task_id ] = "This didn't exist before" response . status_code = status . H ...
https://man.mtool.work/fastapi/0.115/advanced/response-change-status-code/
カスタムレスポンス - HTML、ストリーム、ファイル、その他のレスポンス 9670
カスタムレスポンス - HTML、ストリーム、ファイル、その他のレスポンス デフォルトでは、 FastAPI は JSONResponse を使ってレスポンスを返します。 レスポンスを直接返す で見たように、 Response を直接返すことでこの挙動をオーバーライドできます。 しか ... し、 Response を直接返すと、データは自動的に変換されず、ドキュメントも自動生成されません (例えば、生成されるOpenAPIの一部としてHTTPヘッダ ... ー Content-Type に特定の「メディアタイプ」を含めるなど) 。 しかし、 path operationデコレータ に、使いたい Response を宣言することもできます。 path operation関数 から返されるコンテンツは、その Response に含まれます。 そしてもし、 Response ... が、 JSONResponse や UJSONResponse の場合のようにJSONメディアタイプ ( application/json ) ならば、データは path operationデコレータ に宣言した ...
https://man.mtool.work/fastapi/0.115/advanced/custom-response/
Additional Responses in OpenAPI 9625
Additional Responses in OpenAPI Warning The current page still doesn't have a translation for this language. But you can help translating it: Contributing . Warning This is a rather advanced topic. If you are starting with FastAPI , you mig ... ht not need this. You can declare additional responses, with additional status codes, media types, descriptions, etc. Those additional responses will be included in the OpenAPI ... schema, so they will also appear in the API docs. But for those additional responses you have to make sure you return a Response like JSONResponse directly, with your status code and content. Additional Res ... ponse with model You can pass to your path operation decorators a parameter responses . It receives a dict : the keys are status codes for each response (like 200 ), and the values are other dict s with the ...
https://man.mtool.work/fastapi/0.115/advanced/additional-responses/
PREV 1 2 3 4 5 NEXT