If a function returns a string, just display it

This commit is contained in:
nemunaire 2016-01-13 13:22:03 +01:00
commit 77fcaa39ff

View file

@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"io"
"log" "log"
"net/http" "net/http"
"strings" "strings"
@ -82,7 +83,10 @@ func (a apiRouting) ServeHTTP(w http.ResponseWriter, r *http.Request) {
resStatus = http.StatusNotFound resStatus = http.StatusNotFound
} }
if j, err := json.Marshal(ret); err != nil { if str, found := ret.(string); found {
w.WriteHeader(resStatus)
io.WriteString(w, str)
} else if j, err := json.Marshal(ret); err != nil {
http.Error(w, fmt.Sprintf("{errmsg:\"%q\"}", err), http.StatusInternalServerError) http.Error(w, fmt.Sprintf("{errmsg:\"%q\"}", err), http.StatusInternalServerError)
} else { } else {
w.WriteHeader(resStatus) w.WriteHeader(resStatus)