Add xml response

This commit is contained in:
Eliot Whalan 2016-06-19 16:17:28 +10:00
parent eb6c9ad1fb
commit 4fb704c002
No known key found for this signature in database
GPG Key ID: C0A42175139840D6
1 changed files with 17 additions and 7 deletions

16
main.go
View File

@ -5,6 +5,7 @@ import (
"database/sql"
"encoding/base64"
"encoding/json"
"encoding/xml"
"fmt"
"html"
"io"
@ -101,9 +102,6 @@ func saveHandler(w http.ResponseWriter, r *http.Request) {
}
values := save(buf)
switch output {
case "json":
b := &Response{
ID: values[0],
HASH: values[1],
@ -112,6 +110,9 @@ func saveHandler(w http.ResponseWriter, r *http.Request) {
DELKEY: values[4],
}
switch output {
case "json":
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")