Results of 1 - 10 of about 15 for HTTPException (0.014 sec.)
- SQL (Relational) Databases 11424
- 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
...rt SQLModel and create a database model: Python 3.10+ from typing import Annotated from fastapi import Depends , FastAPI , HTTPException , Query from sqlmodel import Field , Session , SQLModel , create_engine , select class Hero ( SQLModel , table = True ): i...Code below omitted ? ? Full file preview Python 3.10+ from typing import Annotated from fastapi import Depends , FastAPI , HTTPException , Query from sqlmodel import Field , Session , SQLModel , create_engine , select class Hero ( SQLModel , table = True ): i...f read_hero ( hero_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 ( he... - https://man.mtool.work/fastapi/0.115/tutorial/sql-databases/
- 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
...pends in the path 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...rrent_user ( token : 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-Authenti...rrent_active_user ( 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... - https://man.mtool.work/fastapi/0.115/tutorial/security/simple-oauth2/
- OAuth2 scopes 10588
- 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
...ime import datetime , timedelta , timezone from typing import Annotated import jwt from fastapi import Depends , FastAPI , HTTPException , Security , status from fastapi.security import ( OAuth2PasswordBearer , OAuth2PasswordRequestForm , SecurityScopes , ) f..._value = f 'Bearer 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...is None : raise 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" : auth... - https://man.mtool.work/fastapi/0.115/advanced/security/oauth2-scopes/
- path operationデコレータの依存関係 10379
- path operationデコレータの依存関係 場合によっては path operation関数 の中で依存関係の戻り値を本当に必要としないこともあります。 もしくは、依存関係が値を返さない場合もあります。 しかし、それでも実行・解決する必要があります。 このような場合、
...ます。 それは Depends() の list であるべきです: Python 3.8+ - non-Annotated from fastapi import Depends , FastAPI , Header , HTTPException app = FastAPI () async def verify_token ( x_token : str = Header ()): if x_token != "fake-super-secret-token" : raise HTTP...= "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 ( v...ersions and variants Python 3.9+ Python 3.8+ from typing import Annotated from fastapi import Depends , FastAPI , Header , HTTPException app = FastAPI () async def verify_token ( x_token : Annotated [ str , Header ()]): if x_token != "fake-super-secret-token"... - https://man.mtool.work/fastapi/0.115/tutorial/dependencies/dependencies-in-path-...
- エラーハンドリング 10188
- エラーハンドリング APIを使用しているクライアントにエラーを通知する必要がある状況はたくさんあります。 このクライアントは、フロントエンドを持つブラウザ、誰かのコード、IoTデバイスなどが考えられます。 クライアントに以下のようなことを伝える必要が
...タスコードは、クライアントからのエラーがあったことを意味します。 "404 Not Found" のエラー(およびジョーク)を覚えていますか? HTTPException の使用 HTTPレスポンスをエラーでクライアントに返すには、 HTTPException を使用します。 HTTPException のインポート Python 3.8...+ from fastapi import FastAPI , HTTPException app = FastAPI () items = { "foo" : "The Foo Wrestlers" } @app . get ( "/items/ {item_id} " ) async def read_item ( item_id...: str ): if item_id not in items : raise HTTPException ( status_code = 404 , detail = "Item not found" ) return { "item" : items [ item_id ]} コード内での HTTPException の発生 H... - https://man.mtool.work/fastapi/0.115/tutorial/handling-errors/
- パスワード(およびハッシュ化)によるOAuth2、JWTトークンによるBearer 10118
- パスワード(およびハッシュ化)によるOAuth2、JWTトークンによるBearer これでセキュリティの流れが全てわかったので、 JWT トークンと安全なパスワードのハッシュ化を使用して、実際にアプリケーションを安全にしてみましょう。 このコードは、アプリケーシ
...atetime import datetime , 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 InvalidT...ORITHM ) return 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...sync def get_current_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_t... - https://man.mtool.work/fastapi/0.115/tutorial/security/oauth2-jwt/
- テスト 8551
- テスト Starlette のおかげで、 FastAPI アプリケーションのテストは簡単で楽しいものになっています。 HTTPX がベースなので、非常に使いやすく直感的です。 これを使用すると、 FastAPI と共に pytest を直接利用できます。 TestClient を使用 TestClient を
...れらの path operation には X-Token ヘッダーが必要です。 Python 3.10+ - non-Annotated from fastapi import FastAPI , Header , HTTPException from pydantic import BaseModel fake_secret_token = "coneofsilence" fake_db = { "foo" : { "id" : "foo" , "title" : "Foo" ,...e_model = Item ) 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 =...e_model = Item ) async def create_item ( item : Item , x_token : str = Header ()): if x_token != fake_secret_token : raise HTTPException ( status_code = 400 , detail = "Invalid X-Token header" ) if item . id in fake_db : raise HTTPException ( status_code = 40... - https://man.mtool.work/fastapi/0.115/tutorial/testing/
- Global Dependencies 8324
- Global Dependencies Warning The current page still doesn't have a translation for this language. But you can help translating it: Contributing . For some types of applications you might want to add dependencies to the whole application. Sim
...ill be applied to all the path operations in the application: Python 3.9+ from fastapi import Depends , FastAPI , Header , HTTPException from typing_extensions import Annotated async def verify_token ( x_token : Annotated [ str , Header ()]): if x_token != "f...ake-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 != "fake-super-secret-key" : raise HTTPException ( status_code = 400 , detail = "X-Key header invalid" ) return x_key app = FastAPI ( dependencies = [ Depends ( verify_tok... - https://man.mtool.work/fastapi/0.115/tutorial/dependencies/global-dependencies/
- Exceptions - HTTPException and WebSocketException 8098
- Exceptions - HTTPException and WebSocketException These are the exceptions that you can raise to show errors to the client. When you raise an exception, as would happen with normal Python, the rest of the execution is aborted. This way you
...can raise these exceptions from anywhere in the code to abort a request and show the error to the client. You can use: HTTPException WebSocketException These exceptions can be imported directly from fastapi : from fastapi import HTTPException , WebSocketE...xception ::: fastapi.HTTPException ::: fastapi.WebSocketException... - https://man.mtool.work/fastapi/0.115/reference/exceptions/
- HTTP Basic Auth 7889
- 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
....password is "swordfish" . 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_cu...e_digest ( current_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...Other versions and 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 = FastA... - https://man.mtool.work/fastapi/0.115/advanced/security/http-basic-auth/