How to make a file downloadable in FastAPI with supporting multiple extension types?

146 Views Asked by At

I am fetching contents of files from a server. The server contains all types of files like json, text, etc.

@app.get("myurl")
def get_file(myfilename):
  mydata=fetch_file_from_server(myfilename)
  headers = {"content-disposition": "attachment; filename=\"{}\"".format(myfilename)}
  return Response(content=mydata, media_type="application/json", headers=headers)

I tried with media_type="application/json" but it seems it will only work with JSON files.

I expect the file to be downloaded irrespective of the extension.

I am currently using media_type="application/tar+gzip", but not sure if this is the correct way of doing this.

0

There are 0 best solutions below