Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork8.3k
-
First Check
Commit to Help
Example CodefromtypingimportAny,Dict,List,UnionfromfastapiimportFastAPI,Bodyfromstarlette.requestsimportRequestfromstarlette.responsesimportResponseapp=FastAPI()@app.post("/any")# fails with 422asyncdefhandle(request:Any):print(f"got{request}")returnrequest@app.post("/raw")# worksasyncdefhandle(request:Request):print(f"got{request}")returnResponse(content=awaitrequest.body())@app.post("/dict")# worksasyncdefhandle(request:Dict):print(f"got{request}")returnrequest@app.post("/list")# fails with 422asyncdefhandle(request:List):print(f"got{request}")returnrequest@app.post("/anybody")# worksasyncdefhandle(request:Any=Body(None)):print(f"got{request}")returnrequest DescriptionI have what I think to be a bug in FastAPI, or is at least an opportunity to make something clearer in the docs. We are creating a service that basically proxies requests from the outside world to our existing services. Some of those services basically pass requests along to other services. We would like the outer services to have no opinion on the content that is getting passed along, such that request validation happens at the service that actually cares about the request. This reduces duplication and risk of subtle bugs. To us the obvious way to do this was to declare a method parameter of If people agree this is a bug, should I file it as an issue? Operating SystemLinux Operating System DetailsNo response FastAPI Version0.122.0 Pydantic Version2.12.3 Python VersionPython 3.13.5 Additional ContextNo response |
BetaWas this translation helpful?Give feedback.
All reactions
Replies: 2 comments 3 replies
-
@app.post("/any")# fails with 422asyncdefhandle(request:Any):print(f"got{request}")returnrequest That works for me, can I see how are u doing the curl? |
BetaWas this translation helpful?Give feedback.
All reactions
-
Fromhttps://fastapi.tiangolo.com/tutorial/body/#request-body-path-query-parameters
I think this is why |
BetaWas this translation helpful?Give feedback.
All reactions
-
I'm not using curl, but this is the request and response: |
BetaWas this translation helpful?Give feedback.
All reactions
-
Oh okey understood, if you go to the swagger docs, u will see that this param is interpreted by FastAPI as Query Param, since you're not sending it as Query Param ur receiving the 422 error. If you need to pass frompydanticimportJsonValuefromtypingimportAnnotated@app.post("/anybody")asyncdefhandle(request:Annotated[JsonValue,Body()]):print(f"got{request}")returnrequest |
BetaWas this translation helpful?Give feedback.
All reactions
-
Why not typing and using/passing around the |
BetaWas this translation helpful?Give feedback.