I have an endpoint where I need to serve the templates (csv files)
I have written the code for the same
func main() {
router := InitRouter()
log.Fatal(http.ListenAndServe(":8000", router))
}
func InitRouter() http.Handler {
r := mux.NewRouter()
r.HandleFunc("/downloadTemplate", DownloadTemplate).Methods(http.MethodGet)
return r
}
func DownloadTemplate(res http.ResponseWriter, req *http.Request) {
templateName := strings.TrimSpace(req.URL.Query().Get("templateName"))
fileExtension := strings.TrimSpace(req.URL.Query().Get("fileExtension"))
baseDirectoryPath, _ := os.Getwd()
filePath := filepath.Join(baseDirectoryPath, "files", "templates")
fileName := templateName + fileExtension
res.Header().Set("Content-Disposition", mime.FormatMediaType("attachment", map[string]string{"filename": fileName}))
res.Header().Set("Content-Type", "application/octet-stream")
http.ServeFile(res, req, filepath.Join(filePath, fileName))
}
I am able to get the required file on my local machine
But when I hit the same endpoint on ECS, it returns 404 page not found
Response headers are:
{
"Content-Disposition": "attachment; filename=filename.csv",
"Content-Type": "text/plain; charset=utf-8",
"Content-Length": 19
}
Here, Content-Disposition header has been set as it should be. But, the "Content-Type" header should have been "application/octet-stream" which is not
What am I missing here? Is there anything else which needs to be done to serve files from the golang server?