コンテンツにスキップ
per page, with , order by , clip by
Results of 1 - 10 of about 50 for json (0.002 sec.)
テスト 13520
テスト Starlette のおかげで、 FastAPI アプリケーションのテストは簡単で楽しいものになっています。 HTTPX がベースなので、非常に使いやすく直感的です。 これを使用すると、 FastAPI と共に pytest を直接利用できます。 TestClient を使用 TestClient を ... ient ( app ) def test_read_main (): response = client . get ( "/" ) assert response . status_code == 200 assert response . json () == { "msg" : "Hello World" } 豆知識 テスト関数は async def ではなく、通常の def であることに注意してください。 また、ク ... ient ( app ) def test_read_main (): response = client . get ( "/" ) assert response . status_code == 200 assert response . json () == { "msg" : "Hello World" } テスト: 例の拡張 次に、この例を拡張し、詳細を追加して、さまざまなパーツをテストする方法を確 ... t . get ( "/items/foo" , headers = { "X-Token" : "coneofsilence" }) assert response . status_code == 200 assert response . json () == { "id" : "foo" , "title" : "Foo" , "description" : "There goes my hero" , } def test_read_item_bad_token (): respons ...
https://man.mtool.work/fastapi/0.115/tutorial/testing/
JSON互換エンコーダ 11640
JSON互換エンコーダ データ型(Pydanticモデルのような)をJSONと互換性のあるもの( dict や list など)に変更する必要がある場合があります。 例えば、データベースに保存する必要がある場合です。 そのために、 FastAPI は jsonable_encoder() 関数を提供 ... しています。 jsonable_encoder の使用 JSON互換のデータのみを受信するデータベース fase_db があるとしましょう。 例えば、 datetime オブジェクトは ... せん。 同様に、このデータベースはPydanticモデル(属性を持つオブジェクト)を受け取らず、 dict だけを受け取ります。 そのために jsonable_encoder を使用することができます。 Pydanticモデルのようなオブジェクトを受け取り、JSON互換版を返します: Python 3.8+ fro ... m datetime import datetime from typing import Union from fastapi import FastAPI from fastapi.encoders import jsonable_encoder from pydantic import BaseModel fake_db = {} class Item ( BaseModel ): title : str timestamp : datetime descrip ...
https://man.mtool.work/fastapi/0.115/tutorial/encoder/
Additional Responses in OpenAPI 11475
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 ... 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 Response with model You can pass to your path operation de ... can have a key model , containing a Pydantic model, just like response_model . FastAPI will take that model, generate its JSON Schema and include it in the correct place in OpenAPI. For example, to declare another response with a status code 404 and ... a Pydantic model Message , you can write: Python 3.8+ from fastapi import FastAPI from fastapi.responses import JSONResponse from pydantic import BaseModel class Item ( BaseModel ): id : str value : str class Message ( BaseModel ): message ...
https://man.mtool.work/fastapi/0.115/advanced/additional-responses/
Testing Dependencies with Overrides 9911
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 ... ef 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_w ... ith_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_ ... (): response = client . get ( "/items/?q=foo&skip=100&limit=200" ) assert response . status_code == 200 assert response . json () == { "message" : "Hello Items!" , "params" : { "q" : "foo" , "skip" : 5 , "limit" : 10 }, } ? Other versions and varian ...
https://man.mtool.work/fastapi/0.115/advanced/testing-dependencies/
Generate Clients 9535
Generate Clients Warning The current page still doesn't have a translation for this language. But you can help translating it: Contributing . As FastAPI is based on the OpenAPI specification, you get automatic compatibility with many tools, ... ed in the local project, you probably wouldn't be able to call that command directly, but you would put it on your package.json file. It could look like this: { "name" : "frontend-app" , "version" : "1.0.0" , "description" : "" , "main" : "index.js" ... , "scripts" : { "generate-client" : "openapi-ts --input http://localhost:8000/openapi.json --output ./src/client --client axios" }, "author" : "" , "license" : "" , "devDependencies" : { "@hey-api/openapi-ts" : "^ ... -client frontend-app@1.0.0 generate-client /home/user/code/frontend-app > openapi-ts --input http://localhost:8000/openapi.json --output ./src/client --client axios That command will generate code in ./src/client and will use axios (the frontend HTTP ...
https://man.mtool.work/fastapi/0.115/advanced/generate-clients/
スキーマの追加 - 例 8948
スキーマの追加 - 例 JSON Schemaに追加する情報を定義することができます。 一般的なユースケースはこのドキュメントで示されているように example を追加することです。 JSON Schemaの追加情報を宣言する方法はいくつかあります。 Pydanticの schema_extra ... : name : str description : Union [ str , None ] = None price : float tax : Union [ float , None ] = None model_config = { "json_schema_extra" : { "examples" : [ { "name" : "Foo" , "description" : "A very nice Item" , "price" : 35.4 , "tax" : 3.2 , } ... s Item ( BaseModel ): name : str description : str | None = None price : float tax : float | None = None model_config = { "json_schema_extra" : { "examples" : [ { "name" : "Foo" , "description" : "A very nice Item" , "price" : 35.4 , "tax" : 3.2 , } ... id : int , item : Item ): results = { "item_id" : item_id , "item" : item } return results その追加情報はそのまま出力され、JSON Schemaに追加されます。 Field の追加引数 後述する Field 、 Path 、 Query 、 Body などでは、任意の引数を関数に渡すことでJSON ...
https://man.mtool.work/fastapi/0.115/tutorial/schema-extra-example/
最初のステップ 8888
最初のステップ 最もシンプルなFastAPIファイルは以下のようになります: Python 3.8+ from fastapi import FastAPI app = FastAPI () @app . get ( "/" ) async def root (): return { "message" : "Hello World" } これを main.py にコピーします。 ライブサ ... はローカルマシンでアプリが提供されているURLを示しています。 チェック ブラウザで http://127.0.0.1:8000 を開きます。 次のようなJSONレスポンスが表示されます: { "message" : "Hello World" } 対話的APIドキュメント 次に、 http://127.0.0.1:8000/docs にアクセスし ... 様です。 このスキーマ定義はAPIパス、受け取り可能なパラメータなどが含まれます。 データ「スキーマ」 「スキーマ」という用語は、JSONコンテンツなどの一部のデータの形状を指す場合もあります。 そのような場合、スキーマはJSON属性とそれらが持つデータ型などを意味し ... ます。 OpenAPIおよびJSONスキーマ OpenAPIはAPIのためのAPIスキーマを定義します。そして、そのスキーマは JSONデータスキーマ の標準規格に準拠したJSONスキ ...
https://man.mtool.work/fastapi/0.115/tutorial/first-steps/
フォームデータ 8677
フォームデータ JSONの代わりにフィールドを受け取る場合は、 Form を使用します。 情報 フォームを使うためには、まず python-multipart をインストールします。 たとえば、 pip install python-multipart のように。 Form のインポート fastapi から Form を ... ルドとして username と password を送信する必要があります。 仕様 では、フィールドの名前が username と password であることと、JSONではなくフォームフィールドとして送信されることを要求しています。 Form では Body (および Query や Path 、 Cookie )と同じメタ ... ィを宣言するには、明示的に Form を使用する必要があります。なぜなら、これを使わないと、パラメータはクエリパラメータやボディ(JSON)パラメータとして解釈されるからです。 「フォームフィールド」について HTMLフォーム( <form></form> )がサーバにデータを送信す ... る方法は、通常、そのデータに「特別な」エンコーディングを使用していますが、これはJSONとは異なります。 FastAPI は、JSONの代わりにそのデータを適切な場所から読み込むようにします。 技術詳細 フォームからのデータは通 ...
https://man.mtool.work/fastapi/0.115/tutorial/request-forms/
レスポンスを直接返す 8301
レスポンスを直接返す FastAPI の path operation では、通常は任意のデータを返すことができます: 例えば、 dict 、 list 、Pydanticモデル、データベースモデルなどです。 デフォルトでは、 FastAPI は JSON互換エンコーダ で説明されている jsonable_encod ... er により、返す値を自動的にJSONに変換します。 このとき背後では、JSON互換なデータ (例えば dict ) を、クライアントへ送信されるレスポンスとして利用される JSON ... Response の中に含めます。 しかし、 path operation から JSONResponse を直接返すこともできます。 これは例えば、カスタムヘッダーやcookieを返すときに便利です。 Response を返す 実際は、 R ... esponse やそのサブクラスを返すことができます。 豆知識 JSONResponse それ自体は、 Response のサブクラスです。 Response を返した場合は、 FastAPI は直接それを返します。 それは、Pydantic ...
https://man.mtool.work/fastapi/0.115/advanced/response-directly/
Testing WebSockets 8136
Testing WebSockets Warning The current page still doesn't have a translation for this language. But you can help translating it: Contributing . You can use the same TestClient to test WebSockets. For this, you use the TestClient in a with s ... pp . websocket ( "/ws" ) async def websocket ( websocket : WebSocket ): await websocket . accept () await websocket . send_json ({ "msg" : "Hello WebSocket" }) await websocket . close () def test_read_main (): client = TestClient ( app ) response = c ... lient . get ( "/" ) assert response . status_code == 200 assert response . json () == { "msg" : "Hello World" } def test_websocket (): client = TestClient ( app ) with client . websocket_connect ( "/ws" ... ) as websocket : data = websocket . receive_json () assert data == { "msg" : "Hello WebSocket" } Note For more details, check Starlette's documentation for testing WebSock ...
https://man.mtool.work/fastapi/0.115/advanced/testing-websockets/
PREV 1 2 3 4 5 NEXT