Home > front end >  StreamingResponse FASTAPI returns strange file name
StreamingResponse FASTAPI returns strange file name

Time:06-23

I have an API that outputs StreamingReponse (https://fastapi.tiangolo.com/advanced/custom-response/?h=fileresponse#streamingresponse) as zip/gz. When I download the file VIA Swagger, I get a very strange name, for example: application_gz export something=1&something=1&something=Example&archive_type=gz blob https __<ip_address>_aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaaa so basically - with an ip address of the server, a uuid, some names. Is there anyway to change this to be something I decide, or atleast more elegant? thanks!

CodePudding user response:

You can use the Content-Disposition HTTP header to give an alternative file name for the resource. Since StreamingResponse is a subclass of Response, you can set this by using the headers parameter:

return StreamingResponse(fp, headers={'Content-Disposition': 'attachment; filename="yourfilename.zip"'}

You can also use inline instead of attachment if you don't want to force a download but let the client display it directly instead (for example for PDF files).

  • Related