Wondering if anyone can help me out please.
In FastAPI I want to set up an endpoint which returns the contents of a generated csv file as plain text. I don't want the file to be downloaded.
I've tried the following which works just fine however a file download is always initiated.
@app.get('/hosts/last_hour')
def hosts_last_hour():
epoch_start=time.mktime(datetime.now().timetuple())-3600
epoch_end=time.mktime(datetime.now().timetuple())
process_hosts(epoch_start,epoch_end)
def iterate_csv(epoch_start):
with open(f'output/hosts_traffic_{int(epoch_start)}.csv',mode='rb') as csv_file:
yield from csv_file
response = StreamingResponse(iterate_csv(epoch_start), media_type="text/csv")
return(response)
I need the contents of the file to be sent in the response body as text/csv (don't want a downloaded to be initiated and don't want the response in json format). Any ideas how to achieve this?
Thanks in advance.
José