From 84e7f7e95cad176d01e4ff6e5bfe41b37f6f41db Mon Sep 17 00:00:00 2001 From: Eliot Whalan Date: Sun, 19 Jun 2016 16:17:28 +1000 Subject: [PATCH] Add xml response --- main.go | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/main.go b/main.go index 403094f..f1488cc 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ import ( "database/sql" "encoding/base64" "encoding/json" + "encoding/xml" "fmt" "html" "io" @@ -101,16 +102,16 @@ func saveHandler(w http.ResponseWriter, r *http.Request) { } values := save(buf) + b := &Response{ + ID: values[0], + HASH: values[1], + URL: values[2], + SIZE: len(values[3]), + DELKEY: values[4], + } switch output { case "json": - b := &Response{ - ID: values[0], - HASH: values[1], - URL: values[2], - SIZE: len(values[3]), - DELKEY: values[4], - } w.Header().Set("Content-Type", "application/json") err := json.NewEncoder(w).Encode(b) @@ -119,6 +120,15 @@ func saveHandler(w http.ResponseWriter, r *http.Request) { http.Error(w, err.Error(), http.StatusInternalServerError) return } + case "xml": + x, err := xml.MarshalIndent(b, "", " ") + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + w.Header().Set("Content-Type", "application/xml") + w.Write(x) default: io.WriteString(w, values[2]+"\n")