The body of an HTTP PUT request is a JSON list - like this:
[item1, item2, item3, ...]
I can't change this. (If the root was a JSON object rather than a list there would be no problem.)
Using FastAPI I seem to be unable to access this content in the normal way:
@router.put('/data')
def set_data(data: DataModel): # This doesn't work; how do I even declare DataModel?
I found the following workaround, which seems like a very ugly hack:
class DataModel(BaseModel):
__root__: List[str]
from fastAPI import Request
@router.put('/data')
async def set_data(request: Request): # Get the request object directly
data = DataModel(__root__=await request.json())
This surely can't be the 'approved' way to achieve this. I've scoured the documentation both of FastAPI and pydantic. What am I missing?