HTTP.setheaders for file download

This is the code of web server. In this rest api response, I want the text to be downloaded in the file named results.csv (i.e. user should get the file “results.csv” in the Downloads folder).

Here is the code:

if lastUriItem == "table"
    http = HTTP.Response(200, sText)
    # we have to set the response type
    HTTP.setheader(http, "Content-Type" => "application/force-download")
    HTTP.setheader(http, "Content-Disposition" => "attachment");
    HTTP.setheader(http, "filename" => "results.csv");
    HTTP.setheader(http, "Content-Transfer-Encoding" => "binary");
    HTTP.setheader(http, "Content-Length" => string(length(sText)));
    return http
end

When I type in exact URL to the browser, it downloads file “table” (no extension), and it contains the text I provided as sText.
When I use this in a swagger-ui (and this may be a problem in the swagger code, I don’t know), the name of the downloaded file is something very long & horrible, with parts of URL, e.g.:
application_force-download_table_forecastType=outOfSampleRows&forecastType=production&modelIndex=1_blob_http___localhost_8080_64505337-ef14-46fe-89f8-9af8dbc2c536
(again, no extension)

Is there something better I could do in my code make downloaded file to be “results.csv”?

Thank you,

You specify the filename in the Content-Disposition header. So it should be:

 HTTP.setheader(http, "Content-Disposition" => "attachment; filename=\"results.csv\"");

You might find this helpful:

1 Like