I want my post request response on this format, I'm using FastAPI:
{
"message": "",
"status": 200,
"data": {}
}
So I declare a class to specify my response format and send this model in the response_model parameter. The thing is that in my case data key is a CSV file that's why on post request I have this:
@app.post('/some-path', response_model = ResponseBase)
async def some_path():
....
new_df = pd.DataFrame(final_data, columns=header)
stream = io.StringIO()
new_df.to_csv(stream, index=False)
my_data: Response = StreamingResponse(
iter([stream.getvalue()]), media_type="text/csv"
)
return ResponseBase(
message="Some message",
status = status.HTTP_200_OK,
data = my_data
)
Response model class, should be something like this:
class ResponseBase(BaseModel):
message: str
status:
data:
But I wonder how to specify that status comes from HTTP status code module and data comes from StreamingResponse class.