mirror of https://github.com/tiangolo/fastapi.git
Fix `custom_request_and_route/tutorial002_an**`
This commit is contained in:
parent
8222a163b2
commit
0eef5aa5f4
|
|
@ -92,11 +92,11 @@ We can also use this same approach to access the request body in an exception ha
|
|||
|
||||
All we need to do is handle the request inside a `try`/`except` block:
|
||||
|
||||
{* ../../docs_src/custom_request_and_route/tutorial002_an_py310.py hl[13,15] *}
|
||||
{* ../../docs_src/custom_request_and_route/tutorial002_an_py310.py hl[14,16] *}
|
||||
|
||||
If an exception occurs, the`Request` instance will still be in scope, so we can read and make use of the request body when handling the error:
|
||||
|
||||
{* ../../docs_src/custom_request_and_route/tutorial002_an_py310.py hl[16:18] *}
|
||||
{* ../../docs_src/custom_request_and_route/tutorial002_an_py310.py hl[17:19] *}
|
||||
|
||||
## Custom `APIRoute` class in a router { #custom-apiroute-class-in-a-router }
|
||||
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ from typing import Callable, List
|
|||
from fastapi import Body, FastAPI, HTTPException, Request, Response
|
||||
from fastapi.exceptions import RequestValidationError
|
||||
from fastapi.routing import APIRoute
|
||||
from typing_extensions import Annotated
|
||||
|
||||
|
||||
class ValidationErrorLoggingRoute(APIRoute):
|
||||
|
|
@ -25,5 +26,5 @@ app.router.route_class = ValidationErrorLoggingRoute
|
|||
|
||||
|
||||
@app.post("/")
|
||||
async def sum_numbers(numbers: List[int] = Body()):
|
||||
async def sum_numbers(numbers: Annotated[List[int], Body()]):
|
||||
return sum(numbers)
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
from collections.abc import Callable
|
||||
from typing import Annotated
|
||||
|
||||
from fastapi import Body, FastAPI, HTTPException, Request, Response
|
||||
from fastapi.exceptions import RequestValidationError
|
||||
|
|
@ -25,5 +26,5 @@ app.router.route_class = ValidationErrorLoggingRoute
|
|||
|
||||
|
||||
@app.post("/")
|
||||
async def sum_numbers(numbers: list[int] = Body()):
|
||||
async def sum_numbers(numbers: Annotated[list[int], Body()]):
|
||||
return sum(numbers)
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
from typing import Callable
|
||||
from typing import Annotated, Callable
|
||||
|
||||
from fastapi import Body, FastAPI, HTTPException, Request, Response
|
||||
from fastapi.exceptions import RequestValidationError
|
||||
|
|
@ -25,5 +25,5 @@ app.router.route_class = ValidationErrorLoggingRoute
|
|||
|
||||
|
||||
@app.post("/")
|
||||
async def sum_numbers(numbers: list[int] = Body()):
|
||||
async def sum_numbers(numbers: Annotated[list[int], Body()]):
|
||||
return sum(numbers)
|
||||
|
|
|
|||
Loading…
Reference in New Issue