Results of 1 - 10 of about 22 for headers (0.006 sec.)
- Custom Response Classes - File, HTML, Redirect, Streaming, etc. 12524
- 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
...ance. ::: 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: - ch...arset - status_code - media_type - body - background - raw_headers - render - init_headers - headers - set_cookie - delete_cookie Starlette Responses ::: fastapi.responses.FileResponse opti...ons: members: - chunk_size - charset - status_code - media_type - body - background - raw_headers - render - init_headers - headers - set_cookie - delete_cookie ::: fastapi.responses.HTMLResponse options: members: - char... - https://man.mtool.work/fastapi/0.115/reference/responses/
- Response Headers 11868
- 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 ( "/heade...rs-and-object/" ) def get_headers ( response : Response ): response . headers [ "X-Cat-Dog" ] = "alone in the world" return { "message" : "Hello World" } An...t will still be used to filter and convert the object you returned. FastAPI will use that temporal response to extract the headers (also cookies and status code), and will put them in the final response that contains the value you returned, filtered by... - https://man.mtool.work/fastapi/0.115/advanced/response-headers/
- Header Parameter Models 11784
- Header Parameter Models Warning The current page still doesn't have a translation for this language. But you can help translating it: Contributing . If you have a group of related header parameters , you can create a Pydantic model to decla
...m typing import Annotated from fastapi import FastAPI , Header from pydantic import BaseModel app = FastAPI () class CommonHeaders ( BaseModel ): host : str save_data : bool if_modified_since : str | None = None traceparent : str | None = None x_tag : l...ist [ str ] = [] @app . get ( "/items/" ) async def read_items ( headers : Annotated [ CommonHeaders , Header ()]): return headers ? Other versions and variants Python 3.9+ Python 3.8+ Python 3.1...import Annotated , Union from fastapi import FastAPI , Header from pydantic import BaseModel app = FastAPI () class CommonHeaders ( BaseModel ): host : str save_data : bool if_modified_since : Union [ str , None ] = None traceparent : Union [ str , Non... - https://man.mtool.work/fastapi/0.115/tutorial/header-param-models/
- テスト 10386
- テスト Starlette のおかげで、 FastAPI アプリケーションのテストは簡単で楽しいものになっています。 HTTPX がベースなので、非常に使いやすく直感的です。 これを使用すると、 FastAPI と共に pytest を直接利用できます。 TestClient を使用 TestClient を
...stClient from .main import app client = TestClient ( app ) def test_read_item (): response = client . get ( "/items/foo" , headers = { "X-Token" : "coneofsilence" }) assert response . status_code == 200 assert response . json () == { "id" : "foo" , "tit..."Foo" , "description" : "There goes my hero" , } def test_read_item_bad_token (): response = client . get ( "/items/foo" , headers = { "X-Token" : "hailhydra" }) assert response . status_code == 400 assert response . json () == { "detail" : "Invalid X-T...oken header" } def test_read_nonexistent_item (): response = client . get ( "/items/baz" , headers = { "X-Token" : "coneofsilence" }) assert response . status_code == 404 assert response . json () == { "detail" : "Item no... - https://man.mtool.work/fastapi/0.115/tutorial/testing/
- OAuth2 scopes 9444
- 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
...als_exception = HTTPException ( status_code = status . HTTP_401_UNAUTHORIZED , detail = "Could not validate credentials" , headers = { "WWW-Authenticate" : authenticate_value }, ) try : payload = jwt . decode ( token , SECRET_KEY , algorithms = [ ALGORI...n_data . scopes : raise HTTPException ( status_code = status . HTTP_401_UNAUTHORIZED , detail = "Not enough permissions" , headers = { "WWW-Authenticate" : authenticate_value }, ) return user async def get_current_active_user ( current_user : Annotated...als_exception = HTTPException ( status_code = status . HTTP_401_UNAUTHORIZED , detail = "Could not validate credentials" , headers = { "WWW-Authenticate" : authenticate_value }, ) try : payload = jwt . decode ( token , SECRET_KEY , algorithms = [ ALGORI... - https://man.mtool.work/fastapi/0.115/advanced/security/oauth2-scopes/
- パスワード(およびハッシュ化)によるOAuth2、JWTトークンによるBearer 9124
- パスワード(およびハッシュ化)によるOAuth2、JWTトークンによるBearer これでセキュリティの流れが全てわかったので、 JWT トークンと安全なパスワードのハッシュ化を使用して、実際にアプリケーションを安全にしてみましょう。 このコードは、アプリケーシ
...als_exception = HTTPException ( status_code = status . HTTP_401_UNAUTHORIZED , detail = "Could not validate credentials" , headers = { "WWW-Authenticate" : "Bearer" }, ) try : payload = jwt . decode ( token , SECRET_KEY , algorithms = [ ALGORITHM ]) use...ot user : raise HTTPException ( status_code = status . HTTP_401_UNAUTHORIZED , detail = "Incorrect username or password" , headers = { "WWW-Authenticate" : "Bearer" }, ) access_token_expires = timedelta ( minutes = ACCESS_TOKEN_EXPIRE_MINUTES ) access_t...als_exception = HTTPException ( status_code = status . HTTP_401_UNAUTHORIZED , detail = "Could not validate credentials" , headers = { "WWW-Authenticate" : "Bearer" }, ) try : payload = jwt . decode ( token , SECRET_KEY , algorithms = [ ALGORITHM ]) use... - https://man.mtool.work/fastapi/0.115/tutorial/security/oauth2-jwt/
- Simple OAuth2 with Password and Bearer 8501
- 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
...ser : raise HTTPException ( status_code = status . HTTP_401_UNAUTHORIZED , detail = "Invalid authentication credentials" , headers = { "WWW-Authenticate" : "Bearer" }, ) return user async def get_current_active_user ( current_user : Annotated [ User , D...ser : raise HTTPException ( status_code = status . HTTP_401_UNAUTHORIZED , detail = "Invalid authentication credentials" , headers = { "WWW-Authenticate" : "Bearer" }, ) return user async def get_current_active_user ( current_user : Annotated [ User , D...ser : raise HTTPException ( status_code = status . HTTP_401_UNAUTHORIZED , detail = "Invalid authentication credentials" , headers = { "WWW-Authenticate" : "Bearer" }, ) return user async def get_current_active_user ( current_user : Annotated [ User , D... - https://man.mtool.work/fastapi/0.115/tutorial/security/simple-oauth2/
- Custom Request and APIRoute class 8349
- 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
...async def body ( self ) -> bytes : if not hasattr ( self , "_body" ): body = await super () . body () if "gzip" in self . headers . getlist ( "Content-Encoding" ): body = gzip . decompress ( body ) self . _body = body return self . _body class GzipRout...async def body ( self ) -> bytes : if not hasattr ( self , "_body" ): body = await super () . body () if "gzip" in self . headers . getlist ( "Content-Encoding" ): body = gzip . decompress ( body ) self . _body = body return self . _body class GzipRout...ime . time () response : Response = await original_route_handler ( request ) duration = time . time () - before response . headers [ "X-Response-Time" ] = str ( duration ) print ( f "route duration: { duration } " ) print ( f "route response: { response... - https://man.mtool.work/fastapi/0.115/how-to/custom-request-and-route/
- Response Cookies 7828
- 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
...e used to filter and convert the object you returned. FastAPI will use that temporal response to extract the cookies (also headers and status code), and will put them in the final response that contains the value you returned, filtered by any response_m...odel . You can also declare the Response parameter in dependencies, and set cookies (and headers) in them. Return a Response directly You can also create cookies when returning a Response directly in your code. To do th...loper. But most of the available responses come directly from Starlette. And as the Response can be used frequently to set headers and cookies, FastAPI also provides it at fastapi.Response . To see all the available parameters and options, check the doc... - https://man.mtool.work/fastapi/0.115/advanced/response-cookies/
- HTTP Basic Auth 7828
- 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
...ssword ): raise HTTPException ( status_code = status . HTTP_401_UNAUTHORIZED , detail = "Incorrect username or password" , headers = { "WWW-Authenticate" : "Basic" }, ) return credentials . username @app . get ( "/users/me" ) def read_current_user ( use...ssword ): raise HTTPException ( status_code = status . HTTP_401_UNAUTHORIZED , detail = "Incorrect username or password" , headers = { "WWW-Authenticate" : "Basic" }, ) return credentials . username @app . get ( "/users/me" ) def read_current_user ( use...ssword ): raise HTTPException ( status_code = status . HTTP_401_UNAUTHORIZED , detail = "Incorrect username or password" , headers = { "WWW-Authenticate" : "Basic" }, ) return credentials . username @app . get ( "/users/me" ) def read_current_user ( use... - https://man.mtool.work/fastapi/0.115/advanced/security/http-basic-auth/