From 69640506d08e02c3e70937f69090904f01da7f25 Mon Sep 17 00:00:00 2001 From: nemunaire Date: Sun, 21 Jan 2018 14:16:31 +0100 Subject: [PATCH] admin/api: bytes handlers now returned data as application/octet-stream --- admin/api/handlers.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/admin/api/handlers.go b/admin/api/handlers.go index e2ab1f00..b61d2d50 100644 --- a/admin/api/handlers.go +++ b/admin/api/handlers.go @@ -24,8 +24,6 @@ func apiHandler(f DispatchFunction) func(http.ResponseWriter, *http.Request, htt } log.Printf("%s \"%s %s\" [%s]\n", r.RemoteAddr, r.Method, r.URL.Path, r.UserAgent()) - w.Header().Set("Content-Type", "application/json") - var ret interface{} var err error = nil @@ -64,14 +62,20 @@ func apiHandler(f DispatchFunction) func(http.ResponseWriter, *http.Request, htt } if str, found := ret.(string); found { + w.Header().Set("Content-Type", "application/json") w.WriteHeader(resStatus) io.WriteString(w, str) } else if bts, found := ret.([]byte); found { + w.Header().Set("Content-Type", "application/octet-stream") + w.Header().Set("Content-Disposition", "attachment") + w.Header().Set("Content-Transfer-Encoding", "binary") w.WriteHeader(resStatus) w.Write(bts) } else if j, err := json.Marshal(ret); err != nil { + w.Header().Set("Content-Type", "application/json") http.Error(w, fmt.Sprintf("{\"errmsg\":%q}", err), http.StatusInternalServerError) } else { + w.Header().Set("Content-Type", "application/json") w.WriteHeader(resStatus) w.Write(j) }