コンテンツにスキップ
per page, with , order by , clip by
Results of 1 - 10 of about 27 for Status (0.012 sec.)
OAuth2 scopes 11668
OAuth2 scopes Warning The current page still doesn't have a translation for this language. But you can help translating it: Contributing . You can use OAuth2 scopes directly with FastAPI , they are integrated to work seamlessly. This would ... elta , timezone from typing import Annotated import jwt from fastapi import Depends , FastAPI , HTTPException , Security , status from fastapi.security import ( OAuth2PasswordBearer , OAuth2PasswordRequestForm , SecurityScopes , ) from jwt.exceptions i ... er scope=" { security_scopes . scope_str } "' else : authenticate_value = "Bearer" credentials_exception = HTTPException ( status_code = status . HTTP_401_UNAUTHORIZED , detail = "Could not validate credentials" , headers = { "WWW-Authenticate" : authe ... credentials_exception for scope in security_scopes . scopes : if scope not in token_data . scopes : raise HTTPException ( status_code = status . HTTP_401_UNAUTHORIZED , detail = "Not enough permissions" , headers = { "WWW-Authenticate" : authenticate_ ...
https://man.mtool.work/fastapi/0.115/advanced/security/oauth2-scopes/
Simple OAuth2 with Password and Bearer 11059
Simple OAuth2 with Password and Bearer Warning The current page still doesn't have a translation for this language. But you can help translating it: Contributing . Now let's build from the previous chapter and add the missing parts to have ... h operation for /token : Python 3.10+ from typing import Annotated from fastapi import Depends , FastAPI , HTTPException , status from fastapi.security import OAuth2PasswordBearer , OAuth2PasswordRequestForm from pydantic import BaseModel fake_users_db ... en : Annotated [ str , Depends ( oauth2_scheme )]): user = fake_decode_token ( token ) if not user : raise HTTPException ( status_code = status . HTTP_401_UNAUTHORIZED , detail = "Invalid authentication credentials" , headers = { "WWW-Authenticate" : " ... r ( current_user : Annotated [ User , Depends ( get_current_user )], ): if current_user . disabled : raise HTTPException ( status_code = 400 , detail = "Inactive user" ) return current_user @app . post ( "/token" ) async def login ( form_data : Annotat ...
https://man.mtool.work/fastapi/0.115/tutorial/security/simple-oauth2/
Status Codes 10401
Status Codes You can import the status module from fastapi : from fastapi import status status is provided directly by Starlette. It contains a group of named constants (variables) with integer status codes. For example: 200: status.HTTP_20 ... 0_OK 403: status.HTTP_403_FORBIDDEN etc. It can be convenient to quickly access HTTP (and WebSocket) status codes in your app, using autoco ... mpletion for the name without having to remember the integer status codes by memory. Read more about it in the FastAPI docs about Response Status Code . Example from fastapi import FastAPI , ... status app = FastAPI () @app . get ( "/items/" , status_code = status . HTTP_418_IM_A_TEAPOT ) def read_items (): return [{ "name ...
https://man.mtool.work/fastapi/0.115/reference/status/
パスワード(およびハッシュ化)によるOAuth2、JWTトークンによるBearer 10335
パスワード(およびハッシュ化)によるOAuth2、JWTトークンによるBearer これでセキュリティの流れが全てわかったので、 JWT トークンと安全なパスワードのハッシュ化を使用して、実際にアプリケーションを安全にしてみましょう。 このコードは、アプリケーシ ... atetime , timedelta , timezone from typing import Union import jwt from fastapi import Depends , FastAPI , HTTPException , status from fastapi.security import OAuth2PasswordBearer , OAuth2PasswordRequestForm from jwt.exceptions import InvalidTokenError ... encoded_jwt async def get_current_user ( token : str = Depends ( oauth2_scheme )): credentials_exception = HTTPException ( status_code = status . HTTP_401_UNAUTHORIZED , detail = "Could not validate credentials" , headers = { "WWW-Authenticate" : "Bear ... rent_active_user ( current_user : User = Depends ( get_current_user )): if current_user . disabled : raise HTTPException ( status_code = 400 , detail = "Inactive user" ) return current_user @app . post ( "/token" ) async def login_for_access_token ( fo ...
https://man.mtool.work/fastapi/0.115/tutorial/security/oauth2-jwt/
SQL (Relational) Databases 10335
SQL (Relational) Databases Warning The current page still doesn't have a translation for this language. But you can help translating it: Contributing . FastAPI doesn't require you to use a SQL (relational) database. But you can use any data ... ro_id : int , session : SessionDep ) -> Hero : hero = session . get ( Hero , hero_id ) if not hero : raise HTTPException ( status_code = 404 , detail = "Hero not found" ) return hero @app . delete ( "/heroes/ {hero_id} " ) def delete_hero ( hero_id : i ... nt , session : SessionDep ): hero = session . get ( Hero , hero_id ) if not hero : raise HTTPException ( status_code = 404 , detail = "Hero not found" ) session . delete ( hero ) session . commit () return { "ok" : True } ? Other vers ... ro_id : int , session : SessionDep ) -> Hero : hero = session . get ( Hero , hero_id ) if not hero : raise HTTPException ( status_code = 404 , detail = "Hero not found" ) return hero @app . delete ( "/heroes/ {hero_id} " ) def delete_hero ( hero_id : i ...
https://man.mtool.work/fastapi/0.115/tutorial/sql-databases/
テスト 10154
テスト Starlette のおかげで、 FastAPI アプリケーションのテストは簡単で楽しいものになっています。 HTTPX がベースなので、非常に使いやすく直感的です。 これを使用すると、 FastAPI と共に pytest を直接利用できます。 TestClient を使用 TestClient を ... sg" : "Hello World" } client = TestClient ( app ) def test_read_main (): response = client . get ( "/" ) assert response . status_code == 200 assert response . json () == { "msg" : "Hello World" } 豆知識 テスト関数は async def ではなく、通常の def であ ... 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" } テスト: 例の拡張 次に、この例を拡張し、詳細を追加して、 ... async def read_main ( item_id : str , x_token : str = Header ()): if x_token != fake_secret_token : raise HTTPException ( status_code = 400 , detail = "Invalid X-Token header" ) if item_id not in fake_db : raise HTTPException ( status_code = 404 , det ...
https://man.mtool.work/fastapi/0.115/tutorial/testing/
Response - Change Status Code 9825
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 ... e cases you need to return a different status code than the default. Use case For example, imagine that you want to return an HTTP status code of "OK" 200 by default. B ... ut if the data didn't exist, you want to create it, and return 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 . Fo ... rameter of type Response in your 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 ...
https://man.mtool.work/fastapi/0.115/advanced/response-change-status-code/
path operationデコレータの依存関係 9315
path operationデコレータの依存関係 場合によっては path operation関数 の中で依存関係の戻り値を本当に必要としないこともあります。 もしくは、依存関係が値を返さない場合もあります。 しかし、それでも実行・解決する必要があります。 このような場合、 ... PI () async def verify_token ( x_token : str = Header ()): if x_token != "fake-super-secret-token" : raise HTTPException ( status_code = 400 , detail = "X-Token header invalid" ) async def verify_key ( x_key : str = Header ()): if x_key != "fake-super- ... secret-key" : raise HTTPException ( status_code = 400 , detail = "X-Key header invalid" ) return x_key @app . get ( "/items/" , dependencies = [ Depends ( verify_tok ... ef verify_token ( x_token : Annotated [ str , Header ()]): if x_token != "fake-super-secret-token" : raise HTTPException ( status_code = 400 , detail = "X-Token header invalid" ) async def verify_key ( x_key : Annotated [ str , Header ()]): if x_key != ...
https://man.mtool.work/fastapi/0.115/tutorial/dependencies/dependencies-in-path-...
追加のステータスコード 9068
追加のステータスコード デフォルトでは、 FastAPI は JSONResponse を使ってレスポンスを返します。その JSONResponse の中には、 path operation が返した内容が入ります。 それは、デフォルトのステータスコードか、 path operation でセットしたものを利用 ... いです。itemが存在しない場合は、それらを作成して201 "Created"を返します。 これを達成するには、 JSONResponse をインポートし、 status_code を設定して直接内容を返します。 Python 3.8+ - non-Annotated from typing import Union from fastapi import Body , FastAP ... I , status from fastapi.responses import JSONResponse app = FastAPI () items = { "foo" : { "name" : "Fighters" , "size" : 6 }, "bar" ... "size" ] = size return item else : item = { "name" : name , "size" : size } items [ item_id ] = item return JSONResponse ( status_code = status . HTTP_201_CREATED , content = item ) ? Other versions and variants Python 3.10+ Python 3.9+ Python 3.8+ Pyt ...
https://man.mtool.work/fastapi/0.115/advanced/additional-status-codes/
HTTP Basic Auth 8706
HTTP Basic Auth Warning The current page still doesn't have a translation for this language. But you can help translating it: Contributing . For the simplest cases, you can use HTTP Basic Auth. In HTTP Basic Auth, the application expects a ... ordfish" . Python 3.9+ import secrets from typing import Annotated from fastapi import Depends , FastAPI , HTTPException , status from fastapi.security import HTTPBasic , HTTPBasicCredentials app = FastAPI () security = HTTPBasic () def get_current_use ... nt_password_bytes , correct_password_bytes ) if not ( is_correct_username and is_correct_password ): raise HTTPException ( status_code = status . HTTP_401_UNAUTHORIZED , detail = "Incorrect username or password" , headers = { "WWW-Authenticate" : "Basi ... nd variants Python 3.8+ Python 3.8+ - non-Annotated import secrets from fastapi import Depends , FastAPI , HTTPException , status from fastapi.security import HTTPBasic , HTTPBasicCredentials from typing_extensions import Annotated app = FastAPI () sec ...
https://man.mtool.work/fastapi/0.115/advanced/security/http-basic-auth/
PREV 1 2 3 NEXT