from typing import List, Optional
from fastapi import FastAPI, File, UploadFile
from fastapi.responses import HTMLResponse
from pydantic import BaseModel
app = FastAPI(debug=True)
@app.post("/uploadfiles/")
def create_upload_files(upload_file: UploadFile = File(...)):
json_data = ?? upload_file ??
result = model().calculate(json_data)
return { "estimation": result}
@app.get("/")
async def main():
content = """
<body>
<form action="/uploadfiles/" enctype="multipart/form-data" method="post">
<input name="upload_file" type="file" multiple>
<input type="submit">
</form>
</body>
"""
return HTMLResponse(content=content)
I have the above FastAPI app. I need to upload a json file. Thus the upload_file is a json file. Also the model() instance uses a method calculate that takes as input json data. I struggle on how to decode the upload_file from Fast_API to dictionairy format.
I tried upload_file.read() but this returns a bytes array
Could you please help?