chethankumar MV
chethankumar MV

Reputation: 343

How to call FastAPI file upload route

I have written FastAPI to upload a file but in my new requirement, I have to create CLI to upload a file for that I am calling route function.

@app.post("/uploadfile/")
async def create_upload_file(file: Uploadfile =File(...));
      pd.DataFrame = pd.read_csv(file.file)
      return True

create_upload_file(file=UploadFile(input_file_path))

when i tried to read that file like : claims: pd.DataFrame = pd.read_csv(file.file) i am getting following error

No columns to parse from file

Upvotes: 1

Views: 630

Answers (2)

Alejo Villores
Alejo Villores

Reputation: 41

You can try reading and decoding the content from the file and then using pandas to read csv.

from io import StringIO
import pandas as pd

@app.post("/uploadfile/")
async def create_upload_file(file: Uploadfile =File(...)):
      file = StringIO((await file.read()).decode("utf-8"))
      df = pd.read_csv(file)
      return True

Upvotes: 0

Umang Maheshwari
Umang Maheshwari

Reputation: 61

you can read the csv as following:

from io import BytesIO
df = pd.read_csv(BytesIO(file.file.read()))

Upvotes: 2

Related Questions