The accepted answer works as long as the input is wrapped in a dictionary. That is: started with a {
and ends with a }
. However, that does not cover all valid JSON inputs. For example, the following valid JSON inputs would fail:
true
/ false
1.2
null
"text"
[1,2,3]
In order to have a truly generic JSON input accepted by the endpoint, the following would work:
from typing import Any, Dict, List, Union
from fastapi import FastAPI
app = FastAPI()
@app.post("/")
async def handle(request: Union[List,Dict,Any]=None):
return request
Just using Any
did not work for some reason. When I used it, FastApi was expecting the input from the query arguments, not from the request body.
The =None
makes it accept null
and also an empty body. You can leave that part off and then the request body will be required to be not empty/null.
If you are using Python3.10 then you can get rid of the Union
and write the definition as:
async def handle(request: List | Dict | Any = None):