0

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.

Chris
  • 18,724
  • 6
  • 46
  • 80
  • Removing `media_type` from `Response`, and instead letting the browser determine the MIME type, didn't work? – Chris May 18 '23 at 12:45
  • Please have a look at [this answer](https://stackoverflow.com/a/73672334/17865804) as well (under **MIME Type** section) – Chris May 18 '23 at 12:47
  • If you don't want to provide a specific content-type, use `application/octet-stream` - its only definition is "arbitrary binary data", which generally tells the browser "I'm not saying this is anything specific, but treat it as binary". – MatsLindh May 19 '23 at 18:17

0 Answers0