Results of 1 - 8 of about 8 for JSONResponse (0.001 sec.)
- 追加のステータスコード 13212
- 追加のステータスコード デフォルトでは、 FastAPI は JSONResponse を使ってレスポンスを返します。その JSONResponse の中には、 path operation が返した内容が入ります。 それは、デフォルトのステータスコードか、 path operation でセットしたものを利用
...します。 追加のステータスコード メインのステータスコードとは別に、他のステータスコードを返したい場合は、 Response ( JSONResponse など) に追加のステータスコードを設定して直接返します。 例えば、itemを更新し、成功した場合は200 "OK"のHTTPステータスコードを...しかし、新しいitemも許可したいです。itemが存在しない場合は、それらを作成して201 "Created"を返します。 これを達成するには、 JSONResponse をインポートし、 status_code を設定して直接内容を返します。 Python 3.8+ - non-Annotated from typing import Union from fast...api import Body , FastAPI , status from fastapi.responses import JSONResponse app = FastAPI () items = { "foo" : { "name" : "Fighters" , "size" : 6 }, "bar" : { "name" : "Tenders" , "size" : 3 }} @app... - https://man.mtool.work/fastapi/0.115/advanced/additional-status-codes/
- レスポンスを直接返す 10338
- レスポンスを直接返す FastAPI の path operation では、通常は任意のデータを返すことができます: 例えば、 dict 、 list 、Pydanticモデル、データベースモデルなどです。 デフォルトでは、 FastAPI は JSON互換エンコーダ で説明されている jsonable_encod
...SONに変換します。 このとき背後では、JSON互換なデータ (例えば dict ) を、クライアントへ送信されるレスポンスとして利用される JSONResponse の中に含めます。 しかし、 path operation から JSONResponse を直接返すこともできます。 これは例えば、カスタムヘッダーやcook...ieを返すときに便利です。 Response を返す 実際は、 Response やそのサブクラスを返すことができます。 豆知識 JSONResponse それ自体は、 Response のサブクラスです。 Response を返した場合は、 FastAPI は直接それを返します。 それは、Pydanticモデルの...sponse に対して何も変更を加えないので、コンテンツが準備できていることを保証しなければなりません。 例えば、Pydanticモデルを JSONResponse に含めるには、すべてのデータ型 ( datetime や UUID など) をJSON互換の型に変換された dict に変換しなければなりません。 このよ... - https://man.mtool.work/fastapi/0.115/advanced/response-directly/
- Response Cookies 9037
- 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
...rectly . Then set Cookies in it, and then return it: Python 3.8+ from fastapi import FastAPI from fastapi.responses import JSONResponse app = FastAPI () @app . post ( "/cookie/" ) def create_cookie (): content = { "message" : "Come to the dark side, we have...cookies" } response = JSONResponse ( content = content ) response . set_cookie ( key = "fakesession" , value = "fake-cookie-session-value" ) return response.... So, you will have to make sure your data is of the correct type. E.g. it is compatible with JSON, if you are returning a JSONResponse . And also that you are not sending any data that should have been filtered by a response_model . More info Technical Deta... - https://man.mtool.work/fastapi/0.115/advanced/response-cookies/
- Additional Responses in OpenAPI 8531
- 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 decorators...de 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 : str a...ync def read_item ( item_id : str ): if item_id == "foo" : return { "id" : "foo" , "value" : "there goes my hero" } return JSONResponse ( status_code = 404 , content = { "message" : "Item not found" }) Note Keep in mind that you have to return the JSONRespon... - https://man.mtool.work/fastapi/0.115/advanced/additional-responses/
- Response Headers 8386
- 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
...tly and pass the headers as an additional parameter: Python 3.8+ from fastapi import FastAPI from fastapi.responses import JSONResponse app = FastAPI () @app . get ( "/headers/" ) def get_headers (): content = { "message" : "Hello World" } headers = { "X-Cat...-Dog" : "alone in the world" , "Content-Language" : "en-US" } return JSONResponse ( content = content , headers = headers ) Technical Details You could also use from starlette.responses import Response or...from starlette.responses import JSONResponse . FastAPI provides the same starlette.responses as fastapi.responses just as a convenience for you, the developer. But mos... - https://man.mtool.work/fastapi/0.115/advanced/response-headers/
- Custom Response Classes - File, HTML, Redirect, Streaming, etc. 7663
- Custom Response Classes - File, HTML, Redirect, Streaming, etc. There are several custom response classes you can use to create an instance and return them directly from your path operations . Read more about it in the FastAPI docs for Cust
...hers . You can import them directly from fastapi.responses : from fastapi.responses import ( FileResponse , HTMLResponse , JSONResponse , ORJSONResponse , PlainTextResponse , RedirectResponse , Response , StreamingResponse , UJSONResponse , ) FastAPI Respons...ere are a couple of custom FastAPI response classes, you can use them to optimize JSON performance. ::: fastapi.responses.UJSONResponse options: members: - charset - status_code - media_type - body - background - raw_headers - render - init_headers - headers...- set_cookie - delete_cookie ::: fastapi.responses.ORJSONResponse options: members: - charset - status_code - media_type - body - background - raw_headers - render - init_headers - headers... - https://man.mtool.work/fastapi/0.115/reference/responses/
- エラーハンドリング 7591
- エラーハンドリング APIを使用しているクライアントにエラーを通知する必要がある状況はたくさんあります。 このクライアントは、フロントエンドを持つブラウザ、誰かのコード、IoTデバイスなどが考えられます。 クライアントに以下のようなことを伝える必要が
...eption_handler() で追加することができます: Python 3.8+ from fastapi import FastAPI , Request from fastapi.responses import JSONResponse class UnicornException ( Exception ): def __init__ ( self , name : str ): self . name = name app = FastAPI () @app . excep...on_handler ( UnicornException ) async def unicorn_exception_handler ( request : Request , exc : UnicornException ): return JSONResponse ( status_code = 418 , content = { "message" : f "Oops! { exc . name } did something. There goes a rainbow..." }, ) @app ....hing. There goes a rainbow..." } 技術詳細 また、 from starlette.requests import Request と from starlette.responses import JSONResponse を使用することもできます。 FastAPI は開発者の利便性を考慮して、 fastapi.responses と同じ starlette.responses を提供していま... - https://man.mtool.work/fastapi/0.115/tutorial/handling-errors/
- カスタムレスポンス - HTML、ストリーム、ファイル、その他のレスポンス 7464
- カスタムレスポンス - HTML、ストリーム、ファイル、その他のレスポンス デフォルトでは、 FastAPI は JSONResponse を使ってレスポンスを返します。 レスポンスを直接返す で見たように、 Response を直接返すことでこの挙動をオーバーライドできます。 しか
...言することもできます。 path operation関数 から返されるコンテンツは、その Response に含まれます。 そしてもし、 Response が、 JSONResponse や UJSONResponse の場合のようにJSONメディアタイプ ( application/json ) ならば、データは path operationデコレータ に宣言した...は何もコンテンツがないことを期待します。そのため、生成されるOpenAPIドキュメントにレスポンスフォーマットが記載されません。 ORJSONResponse を使う 例えば、パフォーマンスを出したい場合は、 orjson をインストールし、 ORJSONResponse をレスポンスとしてセットすることが...ンポートし、 path operationデコレータ に宣言します。 Python 3.8+ from fastapi import FastAPI from fastapi.responses import ORJSONResponse app = FastAPI () @app . get ( "/items/" , response_class = ORJSONResponse ) async def read_items (): return ORJSONResponse... - https://man.mtool.work/fastapi/0.115/advanced/custom-response/
PREV
1
NEXT